bzoj 1196: [HNOI2006]公路修建问题
2016-06-23
难得一见的水题 我竟然wa了好几次,并不想多说什么。
1 #include<cstdio> 2 #include<iostream> 3 #include<cstdlib> 4 #include<cstring> 5 #include<algorithm> 6 #include<queue> 7 #define ll long long 8 #define M 1000009 9 using namespace std; 10 ll read() 11 { 12 char ch=getchar(); 13 ll x=0,f=1; 14 for(;ch<'0'||ch>'9';ch=getchar()) 15 if(ch=='-') 16 f=-1; 17 for(;ch>='0'&&ch<='9';ch=getchar()) 18 x=x*10+ch-'0'; 19 return x*f; 20 } 21 int n,k,m,fa[M],ans=0x7fffffff; 22 struct data 23 { 24 int u,v,v1; 25 }a[M],b[M]; 26 bool cmp(data a1,data a2) 27 { 28 return a1.v1<a2.v1; 29 } 30 int zhao(int x) 31 { 32 if(fa[x]==x) 33 return x; 34 return fa[x]=zhao(fa[x]); 35 } 36 void pan(int x) 37 { 38 int sum=0,mx=0; 39 for(int i=1;i<=n;i++) 40 fa[i]=i; 41 for(int i=1;i<=m;i++) 42 { 43 int a1=zhao(a[i].u),a2=zhao(a[i].v); 44 if(a1!=a2) 45 { 46 fa[a1]=a2; 47 sum++; 48 mx=a[i].v1; 49 if(sum==x) 50 break; 51 } 52 } 53 for(int i=1;i<=m;i++) 54 { 55 int a1=zhao(a[i].u),a2=zhao(a[i].v); 56 if(a1!=a2) 57 { 58 fa[a1]=a2; 59 sum++; 60 mx=max(b[i].v1,mx); 61 } 62 } 63 ans=mx; 64 } 65 int main() 66 { 67 n=read(); 68 k=read(); 69 m=read(); 70 for(int i=1;i<=m;i++) 71 { 72 a[i].u=read(); 73 a[i].v=read(); 74 b[i]=a[i]; 75 a[i].v1=read(); 76 b[i].v1=read(); 77 } 78 sort(a+1,a+m+1,cmp); 79 sort(b+1,b+m+1,cmp); 80 pan(k); 81 printf("%d\n",ans); 82 return 0; 83 }