Command Network OpenJ_Bailian - 3436(最小有向生成树模板题)

链接:

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

题目:

Command Network
Time Limit: 1000MS   Memory Limit: 131072K
Total Submissions: 8922   Accepted: 2609

Description

After a long lasting war on words, a war on arms finally breaks out between littleken’s and KnuthOcean’s kingdoms. A sudden and violent assault by KnuthOcean’s force has rendered a total failure of littleken’s command network. A provisional network must be built immediately. littleken orders snoopy to take charge of the project.

With the situation studied to every detail, snoopy believes that the most urgent point is to enable littenken’s commands to reach every disconnected node in the destroyed network and decides on a plan to build a unidirectional communication network. The nodes are distributed on a plane. If littleken’s commands are to be able to be delivered directly from a node A to another node B, a wire will have to be built along the straight line segment connecting the two nodes. Since it’s in wartime, not between all pairs of nodes can wires be built. snoopy wants the plan to require the shortest total length of wires so that the construction can be done very soon.

Input

The input contains several test cases. Each test case starts with a line containing two integer N (N ≤ 100), the number of nodes in the destroyed network, and M (M ≤ 104), the number of pairs of nodes between which a wire can be built. The next N lines each contain an ordered pair xi and yi, giving the Cartesian coordinates of the nodes. Then follow M lines each containing two integers i and j between 1 and N(inclusive) meaning a wire can be built between node i and node j for unidirectional command delivery from the former to the latter. littleken’s headquarter is always located at node 1. Process to end of file.

Output

For each test case, output exactly one line containing the shortest total length of wires to two digits past the decimal point. In the cases that such a network does not exist, just output ‘poor snoopy’.

Sample Input

4 6
0 6
4 6
0 0
7 20
1 2
1 3
2 3
3 4
3 1
3 2
4 3
0 0
1 0
0 1
1 2
1 3
4 1
2 3

Sample Output

31.19
poor snoopy

Source

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _  ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 10010, INF = 0x7fffffff;
int n, m;
int vis[maxn], inc[maxn], pre[maxn];
double w[105][105];

struct edge
{
    int x, y;
}Edge[maxn];

void dfs(int u)
{
    vis[u] = 1;
    for(int i=1; i<=n; i++)
        if(!vis[i] && w[u][i] < INF)
            dfs(i);
}


double dirmst(int u)
{
    double ans = 0;
     //==  步骤1: 判断能否形成最小树形图,直接dfs遍历 (就是检验一下图是否能够联通)
    dfs(u);
    for(int i=1; i<=n; i++)
        if(!vis[i])
            return -1;
    //== 如果可以形成最小树形图,继续
    mem(vis, 0);
    while(true)
    {
        //== 1. 找最小前驱边
        for(int i=1; i<=n; i++) if(i != u && !inc[i]){
            w[i][i] = INF; pre[i] = i;
            for(int j=1; j<=n; j++) if(!inc[j] && w[j][i] < w[pre[i]][i])
                pre[i] = j;
        }
        //== 2.判断是否有环
        int i;
        for(i=1; i<=n; i++) if(i != u && !inc[i]){
            int j = i, cnt = 0;
            while(j != u && pre[j] != i && cnt <= n) j = pre[j], ++cnt;
            if(j == u || cnt > n) continue;
            break;
        }
        //== 没有找到环,得到答案
        if(i > n)
        {
            for(int i=1; i<=n; i++) if(i != u && !inc[i]) ans += w[pre[i]][i];
            return ans;
        }
        //==  有环,则对这个环进行收缩
        int j = i;
        mem(vis, 0);
        do{
            ans += w[pre[j]][j], j = pre[j], vis[j] = inc[j] = true;
        }while(j != i);
        inc[i] = false; // 环缩成了点i,点i仍然存在

        for(int k=1; k<=n; k++) if(vis[k]){ //在环中的点
            for(int j=1; j<=n; j++) if(!vis[j]){ //不在环中的点
                if(w[i][j] > w[k][j]) w[i][j] = w[k][j]; //更新环的出边
                if(w[j][k] < INF && w[j][k] - w[pre[k]][k] < w[j][i]) //更新换的入边
                    w[j][i] = w[j][k] - w[pre[k]][k];
            }
        }
    }
    return ans;
}

void init()
{
    mem(vis, 0);
    mem(inc, 0);
    rap(i, 0, n)
        rap(j, i, n)
            w[i][j]  = w[j][i] = INF;
}

int main()
{
    while(~scanf("%d%d", &n, &m))
    {
        init();
        rap(i, 1, n)
        {
            scanf("%d%d", &Edge[i].x, &Edge[i].y);
        }
        rap(i, 1, m)
        {
            int a, b;
            scanf("%d%d", &a, &b);
            double c = sqrt((double)(Edge[a].x - Edge[b].x)*(Edge[a].x - Edge[b].x) + (double)(Edge[a].y - Edge[b].y)*(Edge[a].y - Edge[b].y));
            if(w[a][b] > c)
                w[a][b] = c;
        }
        double ans = dirmst(1);
        if(ans < 0) puts("poor snoopy");
        else printf("%.2f\n", ans);

    }

    return 0;
}

猜你喜欢

转载自www.cnblogs.com/WTSRUVF/p/9417370.html