poj--3164(次小生成树)
来源:互联网 发布:中电数据服务 编辑:程序博客网 时间:2024/05/18 01:55
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 60 64 60 07 201 21 32 33 43 13 24 30 01 00 11 21 34 12 3
Sample Output
31.19poor snoopy
代码如下:
#include<stdio.h>#include<string.h>#include<algorithm>#define INF 0x3f3f3f3fusing namespace std;int n,m;int map[110][110];int inMST[110][110];int MST[110][110];int vis[110];int per[110];int low[110];void inin(){for(int i=1;i<=n;i++){ map[i][i]=0;for(int j=1;j<i;j++){map[i][j]=map[j][i]=INF;}}}void getMap(){int a,b,c;for(int i=1;i<=m;i++){scanf("%d%d%d",&a,&b,&c);if(map[a][b]>c){map[a][b]=map[b][a]=c;}}}void slove(){int temp,v,i,j,k,fa,sum,w;v=0;sum=INF;memset(vis,0,sizeof(vis));memset(inMST,0,sizeof(inMST));memset(MST,0,sizeof(MST));for(i=1;i<=n;i++){low[i]=map[1][i]; per[i]=1;}per[1]=0;vis[1]=1;for(i=2;i<=n;i++){temp=INF;for(j=1;j<=n;j++){if(!vis[j]&&low[j]<temp){temp=low[j];k=j;}}//if(temp==INF)return -1;vis[k]=1;v+=temp;fa=per[k];inMST[k][fa]=inMST[fa][k]=1;for(j=1;j<=n;j++){if(j!=k&&vis[j])MST[k][j]=MST[j][k]=max(MST[j][fa],low[k]);if(!vis[j]&&low[j]>map[k][j]){low[j]=map[k][j];per[j]=k;}}}for(i=1;i<=n;i++){for(j=1;j<i;j++){if(!inMST[i][j]&&map[i][j]!=INF){ w=v-MST[i][j]+map[i][j]; if(w<sum)sum=w;}}}// if(sum==INF)printf("%d %d\n",v,v); printf("%d %d\n",v,sum);}int main(){int t;scanf("%d",&t);while(t--){scanf("%d%d",&n,&m);inin();getMap();slove();}return 0;}
- poj--3164(次小生成树)
- 次小生成树 (poj 1679)
- poj 1679(次小生成树)
- poj 1679(次小生成树)
- poj 1679 The Unique MST(次小生成树)
- poj 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST (次小生成树)
- POJ - 1679 The Unique MST (次小生成树)
- poj 1679 The Unique MST (次小生成树)
- poj 1679 The Unique MST(次小生成树)
- POJ 1679The Unique MST(次小生成树)
- poj 1679 The Unique MST(次小生成树)
- POJ - 1679 - The Unique MST (次小生成树)
- poj 1679 The Unique MST (次小生成树)
- POJ 1679 The Unique MST (次小生成树)
- poj 1679 The Unique MST(次小生成树)
- POJ 1679 The Unique MST(次小生成树)
- POJ-1679-The Unique MST(次小生成树)
- python学习
- 【手记】记录接触到的游戏里比较好的点
- textArea 换行怎么保存到数据库
- 乐观锁、悲观锁及memcached实现方案
- Java并发编程:深入剖析ThreadLocal
- poj--3164(次小生成树)
- plsql自治事务在异常中的使用
- symfony
- UIScrollView控件介绍
- 欢迎使用CSDN-markdown编辑器
- leetcode 233. Number of Digit One
- android应用框架理解
- 之前写的几个demo,一起看看吧。
- Jmeter特殊情况一:登录请求中密码加密的情况