Command Network POJ - 3164 最小树形图(有向图的最小生成树 kruskal算法)

  • Command Network

题目链接:http://poj.org/problem?id=3164

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 Nlines 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

题意:求以点1为根的最小树形图。

思路:最小树形图模板题。

不知道为什么直接以1为根,求点1~n的最小树形图,试了很多次,算法部分改了好几次都wa了。

后来改成这种写法,将点的编号减小1,求0~n-1的最小树形图,就ac了。

for(int i=0;i<m;++i){
	int a,b;
	scanf("%d%d",&a,&b);
	e[i].u=a-1;
	e[i].v=b-1;
	e[i].cost=sqrt(pow(no[a-1].x-no[b-1].x,2)+pow(no[a-1].y-no[b-1].y,2));
}

AC代码:

110ms

#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<algorithm>
#include<cmath>
using namespace std;
const double INF=1e9;
typedef long long ll;
const int MAXN=1e3+10;
int n,m;
int visit[MAXN];
int flag[MAXN];//记录环的编号
int pre[MAXN];
int pos;
struct node{
	double x,y;
}no[110];
struct edge{
	int u,v;
	double cost;
}e[MAXN*MAXN];
double in[MAXN];
double Directed_MST(int root, int V, int E);
int main(){
	while(~scanf("%d%d",&n,&m)){
		double ans=0;
		for(int i=0;i<n;++i){
			scanf("%lf%lf",&no[i].x,&no[i].y);
		}
		for(int i=0;i<m;++i){
			int a,b;
			scanf("%d%d",&a,&b);
			e[i].u=a-1;
			e[i].v=b-1;
			e[i].cost=sqrt(pow(no[a-1].x-no[b-1].x,2)+pow(no[a-1].y-no[b-1].y,2));
		}
		ans=Directed_MST(0,n,m);
		if(ans==-1)
			printf("poor snoopy\n");
		else
			printf("%.2f\n",ans);
	}
	return 0;
}

double Directed_MST(int root, int V, int E)
{
    double ans = 0;//存最小树形图总权值
    while(true)
    {
        int i;
        //1.找每个节点的最小入边
        for( i = 0; i < V; i++){
            in[i] = INF;//初始化为无穷大
		}
        for( i = 0; i < E; i++){//遍历每条边
            int u = e[i].u;
            int v = e[i].v;
            if(e[i].cost < in[v] && u != v){//说明顶点v有条权值较小的入边  记录之
                pre[v] = u;//节点u指向v
                in[v] = e[i].cost;//最小入边
                //if(u == root)//这个点就是实际的起点
                //    pos = i;
            }
        }
        for( i = 0; i < V; i++){//判断是否存在最小树形图
            if(i == root)
                continue;
            if(in[i] == INF)
                return -1;//除了根以外有点没有入边,则根无法到达它  说明它是独立的点 一定不能构成树形图
        }
        //2.找环
        int cnt = 0;//记录环数
        memset(flag, -1, sizeof(flag));
        memset(visit, -1, sizeof(visit));
        in[root] = 0;
        for( i = 0; i < V; i++) //标记每个环
        {
            ans += in[i];//记录权值
            int v = i;
            while(visit[v] != i && flag[v] == -1 && v != root){
                visit[v] = i;
                v = pre[v];
            }
            if(v != root && flag[v] == -1){
                for(int u = pre[v]; u != v; u = pre[u]){
                    flag[u] = cnt;//标记节点u为第几个环
				}
                flag[v] = cnt++;
            }
        }
        if(cnt == 0)
            break; //无环   则break
        for( i = 0; i < V; i++){
            if(flag[i] == -1)
                flag[i] = cnt++;
		}
        //3.建立新图   缩点,重新标记
        for( i = 0; i < E; i++)
        {
            int u = e[i].u;
            int v = e[i].v;
            e[i].u = flag[u];
            e[i].v = flag[v];
            if(flag[u] != flag[v])
				e[i].cost -= in[v];
         }
         V = cnt;
         root = flag[root];
    }
    return ans;
}

猜你喜欢

转载自blog.csdn.net/weixin_43821265/article/details/88054883