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;
    }
  • 相关阅读:
    Simulink模块之Zero-Order Hold和Unit Delay的区别
    芯片电源引脚的去耦电容
    STM32中电源引脚
    Simulink:模块参数初始化
    工作笔记1
    至少清楚知道兼容IE8 ie9 ;持续更新
    FROM_UNIXTIME/CONCAT
    采集文章
    文件上传类(引用)
    php文件相关操作
  • 原文地址:https://www.cnblogs.com/GadyPu/p/4773382.html
Copyright © 2011-2022 走看看