本文主要是介绍【POJ3164】【有向图的最小生成树】【自己的模板】,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
Time Limit: 1000MS | Memory Limit: 131072K | |
Total Submissions: 14811 | Accepted: 4259 |
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 <cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <list>
#include <map>
#include <string>
#include <cstdlib>
#include <cstdio>
#include <algorithm>
using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)
#define per(i,a,n) for (int i=n-1;i>=a;i--)
#define mp push_backint n,m;
struct Point
{double x,y;
}P[110];
struct Edge
{int a,b;double c;
}edge[10010];double dis(Point a,Point b)
{return sqrt((a.x-b.x)*(a.x-b.x) + (a.y-b.y)*(a.y-b.y));
}double d[110];
int p[110];
int vis[110];
int id[110];double zhuli(int root,int n,int m,Edge edge[])
{double res = 0;while(true){for(int i=0;i<n;i++){d[i] = 0x3f3f3f3f;p[i] = -1;}for(int i=0;i<m;i++){Edge e = edge[i];if(d[e.b] > e.c){d[e.b] = e.c;p[e.b] = e.a;}}for(int i=0;i<n;i++){if(i != root && p[i] == -1) return 0x3f3f3f3f;}int cnt = 0;for(int i=0;i<n;i++){id[i] = -1;vis[i] = -1;}d[root] = 0;for(int i=0;i<n;i++){//if(i != root) res += d[i];int v = i;while(v!= -1 && vis[v] == -1 && v != root) {vis[v] = i;v = p[v];}if(v != root && v != -1 && vis[v] == i){int tt = v;do{id[tt] = cnt;tt = p[tt];}while(tt != v);cnt ++;}}if(cnt == 0) break;for(int i=0;i<n;i++){if(id[i] == -1) id[i] = cnt ++;}for(int i=0;i<m;){int v = edge[i].b;edge[i].a = id[edge[i].a];edge[i].b = id[edge[i].b];if(edge[i].a != edge[i].b){edge[i++].c -= d[v];}else{//edge[i--] = edge[--m];swap(edge[i],edge[--m]);}}n = cnt;root = id[root];}return res;
}
int main()
{freopen("in.txt","r",stdin);while(scanf("%d%d",&n,&m) != EOF){for(int i=0;i<n;i++){scanf("%lf%lf",&P[i].x,&P[i].y);}for(int i=0;i<m;i++){int u,v;scanf("%d%d",&u,&v);u --;v --;//if(u == v) continue;edge[i].a = u;edge[i].b = v;edge[i].c = dis(P[u],P[v]);if(u == v) edge[i].c = 0x3f3f3f3f;}// cout << "eee" << endl;double ans = zhuli(0,n,m,edge);if(ans == 0x3f3f3f3f){printf("poor snoopy\n");}else{printf("%.2f\n",ans);}}
}
这篇关于【POJ3164】【有向图的最小生成树】【自己的模板】的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!