POJ 3164 Command Network (最小树形图)
| Time Limit:?1000MS | ? | Memory Limit:?131072K |
| Total Submissions:?10136 | ? | Accepted:?2946 |
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 3Sample Output
31.19 poor snoopySource
POJ Monthly--2006.12.31, galaxy http://hi.baidu.com/ggoldengoat/item/778419d01d1dcb3948e1ddb4 http://www.cnblogs.com/zhsl/archive/2013/02/01/2888834.html 解決這些:http://www.cnblogs.com/nanke/archive/2012/04/11/2441725.html 詳細講解:http://blog.sina.com.cn/s/blog_6af663940100ls4h.html這是最小樹形圖的題目,1是根節點,在開始的時候自己建圖。
輸入n,m;代表有n個結點,接下來n行給出結點的坐標。
接下來m行給出i,j兩個整數,代表i到j有連通,求出i到j的坐標距離,最后求最小樹形圖。
用朱劉算法可以解決。
?
#include<iostream> #include<cstdio> #include<cstring> #include<cmath>using namespace std;const int VN=110; const int INF=99999999;double map[VN][VN]; int n,m,vis[VN],pt[VN][2],pre[VN];double Cal(int i,int j){return sqrt(pow(pt[i][0]-pt[j][0],2.0)+pow(pt[i][1]-pt[j][1],2.0)); }void DFS(int u){vis[u]=1;for(int i=1;i<=n;i++)if(!vis[i] && map[u][i]!=INF)DFS(i); }bool Connected(){ //判斷是否連通,如果連通一定存在最小樹形圖。 memset(vis,0,sizeof(vis));DFS(1);for(int i=1;i<=n;i++)if(!vis[i])return 0;return 1; }double ZLEmonds(){int i,j,k;bool circle[VN];double ans=0;memset(circle,0,sizeof(circle));while(1){for(i=2;i<=n;i++){ //找出最短弧集合E0 if(circle[i])continue;pre[i]=i;map[i][i]=INF;for(j=1;j<=n;j++){if(circle[j])continue;if(map[j][i]<map[pre[i]][i])pre[i]=j;}}for(i=2;i<=n;i++){if(circle[i])continue;j=i;memset(vis,0,sizeof(vis));while(!vis[j] && j!=1){vis[j]=1;j=pre[j];}if(j==1) //檢查是否有環,能找到根節點說明沒環 continue;i=j;ans+=map[pre[i]][i];for(j=pre[i];j!=i;j=pre[j]){ //i不用標記,用作后面縮點用 ans+=map[pre[j]][j];circle[j]=1;}for(j=1;j<=n;j++){if(circle[j])continue;if(map[j][i]!=INF)map[j][i]-=map[pre[i]][i];}for(j=pre[i];j!=i;j=pre[j]) //將環中所有的點成點i,改變邊 for(k=1;k<=n;k++){if(circle[k])continue;map[i][k]=min(map[i][k],map[j][k]);if(map[k][j]!=INF)map[k][i]=min(map[k][i],map[k][j]-map[pre[j]][j]);}break;}if(i>n){ //求出最小樹形圖的權值 for(j=2;j<=n;j++){if(circle[j])continue;ans+=map[pre[j]][j];}break;}}return ans; }int main(){//freopen("input.txt","r",stdin);while(~scanf("%d%d",&n,&m)){int i,j;for(i=1;i<=n;i++)for(j=1;j<=n;j++)map[i][j]=INF;for(i=1;i<=n;i++)scanf("%d%d",&pt[i][0],&pt[i][1]);while(m--){scanf("%d%d",&i,&j);map[i][j]=Cal(i,j);}if(!Connected())printf("poor snoopy\n");elseprintf("%.2f\n",ZLEmonds());}return 0; }?
總結
以上是生活随笔為你收集整理的POJ 3164 Command Network (最小树形图)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 【模板】AC自动机(加强版)
- 下一篇: Usb设备驱动3:root hub守护进