Ice_cream ' s World IIITime
limit:3000/1000 MS (java/others) Memory limit:32768/32768 K (java/others)
Total submission (s): 1237 Accepted Submission (s): 408
Problem Descriptionice_cream ' s world becomes stronger and stronger; Every road is built as undirected. The Queen enjoys traveling around her world; The Queen ' s requirement is like II problem, beautifies the roads, by which there was some ways from every city to the CAPI Tal. The project ' s cost should is as less as better.
Inputevery case has a integers n and m (n<=1000, m<=10000) meaning N cities and M roads, the cities numbered 0 ... N-1, following N lines, each line contain three integers s, T and C, meaning s connected with T has a road would cost C.
Outputif Wiskey can ' t satisfy the Queen ' s requirement, you must is output "impossible", otherwise, print the minimum cost In the This project. After every case print one blank.
Sample Input
2 10 1) 104 0
Sample Output
10impossible
Authorwiskey floating on the surface of the island to connect, for the least cost, if not, then output impossible. Code 1 "Kruskal":
#include <cstdio> #include <cstring> #include <cstdlib> #include <algorithm> #include < Iostream> #include <cmath>using namespace std;int n,m;int pre[1010];struct node{int u; int V; int w;}; Node Sb[10010];bool CMP (node A,node b) {return A.W<B.W;} int find (int x) {if (pre[x]==x) return x; Return Pre[x]=find (Pre[x]); }bool join (int x,int y) {int f1,f2; F1=find (x); F2=find (y); if (F1==F2) return false; if (F1!=F2) pre[f1]=f2; return true;} int main () {int sum; while (scanf ("%d%d", &n,&m)!=eof) {sum=0; for (int i=0;i<n;i++) pre[i]=i; for (int i=0;i<m;i++) scanf ("%d%d%d", &SB[I].U,&SB[I].V,&SB[I].W); Sort (sb,sb+m,cmp); for (int i=0;i<m;i++) {if (Join (SB[I].U,SB[I].V)) SUM+=SB[I].W; } int cnt=0; for (int i=0;i<n;i++) {if (pre[i]==i) cnt++; } if (cnt>1) printf ("impossible\n\n"); else printf ("%d\n\n", sum); } return 0;}
Code 2 "Premium Manaus":
#include <cstdio> #include <cstring> #include <algorithm> #include <iostream>using namespace std;const int inf= 0x3f3f3f3f;const int maxb=1010;int map[maxb][maxb];int vis[maxb];int n,m,sum;int a,b,c;void Prime () { int I,J,K,DIS[MAXB]; int min; memset (vis,0,sizeof (VIS)); int Ans=1; Vis[0]=1; for (i=0;i<n;i++) dis[i]=map[0][i]; for (i=0;i<n;i++) {min=inf; for (j=0;j<n;j++) if (!vis[j]&&min>dis[j]) min=dis[k=j]; if (Min==inf) {if (ans==n) printf ("%d\n", sum); Else puts ("impossible"); Break } sum+=min; Vis[k]=1; ans++; for (j=0;j<n;j++) if (!vis[j]&&dis[j]>map[k][j]) dis[j]=map[k][j]; }}int Main () {while (scanf ("%d%d", &n,&m)!=eof) {memset (map,inf,sizeof (map)); sum=0; while (m--) {scanf ("%d%d%d", &a,&b, &c); if (map[a][b]>c) map[a][b]=map[b][a]=c; }//getchar (); Prime (); GetChar (); Puts (""); } return 0;}
Copyright notice: Bo Master lover, outsiders mistakenly touch!!!
Hdoj 2122 Ice_cream ' s World III "minimum spanning tree"