「POJ3164」Command Network
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
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
1 2 |
31.19 poor snoopy |
Source
一直wa,我还有什么可说呢
后来发现g++输出double要%f
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
#include<cstdio> #include<iostream> #include<cstring> #include<cmath> #define inf 0x7fffffff using namespace std; int pre[105],mark[105],vis[105]; double in[105]; int n,m; struct point{double x,y;}p[105]; struct data{int u,v;double w;}e[50001]; 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 work(int root) { double ans=0; while(1) { for(int i=0;i<n;i++)in[i]=inf; for(int i=0;i<m;i++) { int u=e[i].u,v=e[i].v; if(e[i].w<in[v]&&u!=v) {pre[v]=u;in[v]=e[i].w;} } for(int i=0;i<n;i++) { if(i==root)continue; if(in[i]==inf)return -1; } int cnt=0; memset(mark,-1,sizeof(mark)); memset(vis,-1,sizeof(vis)); in[root]=0; for(int i=0;i<n;i++) { ans+=in[i]; int v=i; while(v!=root&&vis[v]!=i&&mark[v]==-1) {vis[v]=i;v=pre[v];} if(v!=root&&mark[v]==-1) { for(int u=pre[v];u!=v;u=pre[u]) mark[u]=cnt; mark[v]=cnt++; } } if(cnt==0)break; for(int i=0;i<n;i++) if(mark[i]==-1)mark[i]=cnt++; for(int i=0;i<m;i++) { int u=e[i].u,v=e[i].v; e[i].u=mark[u];e[i].v=mark[v]; if(mark[u]!=mark[v])e[i].w-=in[v]; } n=cnt;root=mark[root]; } return ans; } int main() { 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++) { scanf("%d%d",&e[i].u,&e[i].v); e[i].u--;e[i].v--; if(e[i].u!=e[i].v) e[i].w=dis(p[e[i].u],p[e[i].v]); else e[i].w=inf; } double ans=work(0); if(ans==-1)printf("poor snoopy\n"); else printf("%.2lf\n",ans); } return 0; } |