zoukankan      html  css  js  c++  java
  • poj 2485 Highways

    题目连接

    http://poj.org/problem?id=2485

    Highways

    Description

    The island nation of Flatopia is perfectly flat. Unfortunately, Flatopia has no public highways. So the traffic is difficult in Flatopia. The Flatopian government is aware of this problem. They're planning to build some highways so that it will be possible to drive between any pair of towns without leaving the highway system. 

    Flatopian towns are numbered from 1 to N. Each highway connects exactly two towns. All highways follow straight lines. All highways can be used in both directions. Highways can freely cross each other, but a driver can only switch between highways at a town that is located at the end of both highways. 

    The Flatopian government wants to minimize the length of the longest highway to be built. However, they want to guarantee that every town is highway-reachable from every other town.

    Input

    The first line of input is an integer T, which tells how many test cases followed. 
    The first line of each case is an integer N (3 <= N <= 500), which is the number of villages. Then come N lines, the i-th of which contains N integers, and the j-th of these N integers is the distance (the distance should be an integer within [1, 65536]) between village i and village j. There is an empty line after each test case.

    Output

    For each test case, you should output a line contains an integer, which is the length of the longest road to be built such that all the villages are connected, and this value is minimum.

    Sample Input

    1

    3
    0 990 692
    990 0 179
    692 179 0

    Sample Output

    692

    最下生成树。。

    #include<algorithm>
    #include<iostream>
    #include<cstdlib>
    #include<cstring>
    #include<cstdio>
    #include<vector>
    #include<queue>
    #include<map>
    using std::map;
    using std::max;
    using std::find;
    using std::pair;
    using std::vector;
    using std::multimap;
    using std::priority_queue;
    #define pb(e) push_back(e)
    #define sz(c) (int)(c).size()
    #define mp(a, b) make_pair(a, b)
    #define all(c) (c).begin(), (c).end()
    #define iter(c) __typeof((c).begin())
    #define cls(arr, val) memset(arr, val, sizeof(arr))
    #define cpresent(c, e) (find(all(c), (e)) != (c).end())
    #define rep(i, n) for(int i = 0; i < (int)n; i++)
    #define tr(c, i) for(iter(c) i = (c).begin(); i != (c).end(); ++i)
    const int N = 510;
    struct P {
        int w, v;
        P(int i = 0, int j = 0) :w(i), v(j) {}
        inline bool operator<(const P &x) const {
            return w > x.w;
        }
    };
    struct Prim {
        struct edge { int to, w, next; }G[(N * N) << 1];
        int tot, vis[N], head[N], mincost[N];
        inline void init() {
            tot = 0, cls(vis, false), cls(head, -1), cls(mincost, 0x3f);
        }
        inline void add_edge(int u, int v, int w) {
            G[tot] = (edge){ v, w, head[u] }; head[u] = tot++;
            G[tot] = (edge){ u, w, head[v] }; head[v] = tot++;
        }
        inline void built(int V) {
            int w;
            rep(i, V) {
                rep(j, V) {
                    scanf("%d", &w);
                    if(i == j) continue;
                    add_edge(i + 1, j + 1, w);
                }
            }
        }
        inline int prim(int s) {
            int ans = -1;
            priority_queue<P> q;
            q.push(P(0, s));
            for(int i = head[s]; ~i; i = G[i].next) {
                mincost[G[i].to] = G[i].w;
                q.push(P(G[i].w, G[i].to));
            }
            vis[s] = true;
            while(!q.empty()) {
                P t = q.top(); q.pop();
                int u = t.v;
                if(vis[u]) continue;
                vis[u] = true;
                ans = max(ans, t.w);
                for(int i = head[u]; ~i; i = G[i].next) {
                    int &d = mincost[G[i].to];
                    if(!vis[G[i].to] && d > G[i].w) {
                        d = G[i].w;
                        q.push(P(G[i].w, G[i].to));
                    }
                }
            }
            return ans;
        }
        inline void solve(int n) {
            init();
            built(n);
            printf("%d
    ", prim(1));
        }
    }go;
    int main() {
    #ifdef LOCAL
        freopen("in.txt", "r", stdin);
        freopen("out.txt", "w+", stdout);
    #endif
        int t, n;
        scanf("%d", &t);
        while(t--) {
            scanf("%d", &n);
            go.solve(n);
        }
        return 0;
    }
  • 相关阅读:
    git版本超前了N个版本且落后了N个版本的解决办法
    CSS3与动画有关的属性transition、animation、transform对比
    禁止选中文本JS
    页面加载中jquery逐渐消失效果实现
    localstorage和sessionstorage上手使用记录
    点击除元素以外的任意地方隐藏元素js
    js准确获取当前页面url网址信息
    301、404、200、304、500HTTP状态
    对事件委托绑定click的事件的解绑
    RabbitMQ的安装和使用Python连接RabbitMQ
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4773382.html
Copyright © 2011-2022 走看看