天天看點

Command Network(POJ 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.

輸入格式

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.

輸出格式

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’.

輸入樣例

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

輸出樣例

31.19

poor snoopy

分析

題目大意是給定n個點的坐标,以及m條有向邊,每條邊的權值為兩點的間距,求能否以1号點為根節點形成一顆最小樹形圖,如果可以輸出這顆樹的權值,否則輸出“poor snoopy”。

典型的最小樹形圖模闆題,正解是通過朱劉算法,對于朱劉算法不了解的可以看看這篇部落格,主要的算法流程是這樣的:

  1. 求各點最小入邊權值in[i]。
  2. 判斷除根節點外是否所有點都有入邊,有則繼續3,無則無法建樹形圖,輸出“poor snoopy”。
  3. 縮點并統計環,如果有環就繼續4,無環則表示已找到最小樹形圖,輸出其權值即可。
  4. 建新圖,對于邊<u,v>,如果點u和v不在一個環路呢,則邊<u,v>的權值減小in[v]。
  5. 繼續從1-4,直到圖中無環。

以下是源代碼。

源程式

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#define MAXN 105
#define MAXM MAXN*MAXN
#define INF 999999999
using namespace std;
struct Pos{
	double x,y;
}pos[MAXN];
struct Edge{
	int u,v;
	double w;
}edge[MAXM];
int id[MAXN],pre[MAXN];
double dis[MAXN];	//最小入邊 
bool vis[MAXN];
double zhuliu(int root,int n,int m)
{
	double res=0;
	while(1){
		for(int i=1;i<=n;i++)	//初始化
			dis[i]=INF;
		for(int i=1;i<=m;i++){	//計算各點最小入邊與前驅節點 
			int u=edge[i].u,v=edge[i].v;
			if(edge[i].w<dis[v]&&u!=v){
				pre[v]=u;
				dis[v]=edge[i].w;
			}
		} 
		dis[root]=0;
		for(int i=1;i<=n;i++)	//判斷能否成樹 
			if(int(dis[i])==INF)
				return -1;
		int cnt=0;	//記錄環數 
		memset(id,-1,sizeof(id));
		memset(vis,false,sizeof(vis));
		for(int i=1;i<=n;i++){	//标記每個環 
			res+=dis[i];	//記錄權值 
			int v=i;
			while(vis[v]!=i&&id[v]==-1&&v!=root){	//尋找有向環
				//三種情況終止:找到相同标記、節點已屬其他環、周遊到根
				vis[v]=i;
				v=pre[v]; 
			}
			if(v!=root&&id[v]==-1){	//找到環
				id[v]=++cnt;
				for(int u=pre[v];u!=v;u=pre[u])
					id[u]=cnt;
			}
		}
		if(cnt==0)	//無環
			break;
		for(int i=1;i<=n;i++)	//對于非環中點
			if(id[i]==-1)
				id[i]=++cnt;
		for(int i=1;i<=m;i++){	//建新圖,縮點重新标記
			int u=edge[i].u,v=edge[i].v;
			edge[i].u=id[u];
			edge[i].v=id[v];
			if(id[u]!=id[v])	//不在一個環内
				edge[i].w-=dis[v]; 
		} 
		n=cnt;
		root=id[root]; 
	}
	return res;
}
double getdis(Pos a,Pos b)
{
	return sqrt((a.x-b.x)*(a.x-b.x)+(a.y-b.y)*(a.y-b.y));
}
int main()
{
	int n,m;
	while(~scanf("%d%d",&n,&m)){
		for(int i=1;i<=n;i++)	//讀入坐标 
			scanf("%lf%lf",&pos[i].x,&pos[i].y);
		for(int i=1;i<=m;i++){	//建邊 
			scanf("%d%d",&edge[i].u,&edge[i].v);
			if(edge[i].u!=edge[i].v)
				edge[i].w=getdis(pos[edge[i].u],pos[edge[i].v]);
			else
				edge[i].w=INF;
		}
		double res=zhuliu(1,n,m);
		if(int(res)==-1)
            printf("poor snoopy\n");
        else
            printf("%.2f\n",res);
	}
    return 0;
}
           

繼續閱讀