pat 顶级 1001 Battle Over Cities - Hard Version (35 分)
Posted zjxxcn
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了pat 顶级 1001 Battle Over Cities - Hard Version (35 分)相关的知识,希望对你有一定的参考价值。
It is vitally important to have all the cities connected by highways in a war. If a city is conquered by the enemy, all the highways from/toward that city will be closed. To keep the rest of the cities connected, we must repair some highways with the minimum cost. On the other hand, if losing a city will cost us too much to rebuild the connection, we must pay more attention to that city.
Given the map of cities which have all the destroyed and remaining highways marked, you are supposed to point out the city to which we must pay the most attention.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 2 numbers N
(≤500), and M
, which are the total number of cities, and the number of highways, respectively. Then M
lines follow, each describes a highway by 4 integers: City1 City2 Cost Status
where City1
and City2
are the numbers of the cities the highway connects (the cities are numbered from 1 to N
), Cost
is the effort taken to repair that highway if necessary, and Status
is either 0, meaning that highway is destroyed, or 1, meaning that highway is in use.
Note: It is guaranteed that the whole country was connected before the war.
Output Specification:
For each test case, just print in a line the city we must protest the most, that is, it will take us the maximum effort to rebuild the connection if that city is conquered by the enemy.
In case there is more than one city to be printed, output them in increasing order of the city numbers, separated by one space, but no extra space at the end of the line. In case there is no need to repair any highway at all, simply output 0.
Sample Input 1:
4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 1 0
Sample Output 1:
1 2
Sample Input 2:
4 5
1 2 1 1
1 3 1 1
2 3 1 0
2 4 1 1
3 4 2 1
Sample Output 2:
0
pat的题目真实坑爹啊,数据范围都不明确!!!!
直接用并查集求最小生成树,把边分成两类,一类是已有的边,代价为0,一类是要修复的边,代价就是修复的代价,然后每次暴力枚举删除哪个电以后求剩下n-1个的最小生成树。
注意这里有一种特殊情况,即如果删除某个点以后无法让剩下n-1个点联通,那么此时的修复代价是最大的,而题目并没有明确这一点。
1 #include<bits/stdc++.h> 2 using namespace std; 3 int const N=500+10; 4 struct edge 5 int x,y,z; 6 bool operator < (const edge &rhs)const 7 return z<rhs.z; 8 9 e[N*N],e2[N*N]; 10 int n,m,f[N],ans[N]; 11 int find(int x) 12 return x==f[x]? x:f[x]=find(f[x]); 13 14 int main() 15 scanf("%d%d",&n,&m); 16 int n1=0,n2=0; 17 for(int i=1;i<=m;i++) 18 int x,y,z,s; 19 scanf("%d%d%d%d",&x,&y,&z,&s); 20 if(s==1) 21 n1++; 22 e[n1]=(edge)x,y,0; 23 else 24 n2++; 25 e2[n2]=(edge)x,y,z; 26 27 28 sort(e+1,e+n1+1); 29 sort(e2+1,e2+n2+1); 30 int cost=-1000000; 31 for(int k=1;k<=n;k++) 32 for(int i=1;i<=n;i++) f[i]=i; 33 int sum=0,c=0; 34 for(int i=1;i<=n1;i++) 35 if(e[i].x==k || e[i].y==k) continue; 36 int fx=find(e[i].x); 37 int fy=find(e[i].y); 38 if(fx!=fy) 39 sum++; 40 f[fx]=fy; 41 42 43 for(int i=1;i<=n2 && sum<n-2;i++) 44 if(e2[i].x==k || e2[i].y==k) continue; 45 int fx=find(e2[i].x); 46 int fy=find(e2[i].y); 47 if(fx!=fy) 48 sum++; 49 f[fx]=fy; 50 c+=e2[i].z; 51 52 53 if(sum<n-2) c=1e9; 54 if(c>cost) 55 ans[0]=1; ans[1]=k;cost=c; 56 else if(c==cost) 57 ans[++ans[0]]=k; 58 59 60 if(cost==0) 61 printf("%d\n",0); 62 else 63 printf("%d",ans[1]); 64 for(int i=2;i<=ans[0];i++) 65 printf(" %d",ans[i]); 66 67 return 0; 68
以上是关于pat 顶级 1001 Battle Over Cities - Hard Version (35 分)的主要内容,如果未能解决你的问题,请参考以下文章
PAT T1001 Battle Over Cities-Hard Version
「日常训练」Battle Over Cities - Hard Version(PAT-TOP-1001)
pat1001. Battle Over Cities - Hard Version 解题报告
PAT-Top1001. Battle Over Cities - Hard Version (35)