Agri-Net
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 49743 | Accepted: 20679 |
Description
Farmer John has been elected mayor of his town! One of his campaign promises was to bring internet connectivity to all farms in the area. He needs your help, of course.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Farmer John ordered a high speed connection for his farm and is going to share his connectivity with the other farmers. To minimize cost, he wants to lay the minimum amount of optical fiber to connect his farm to all the other farms.
Given a list of how much fiber it takes to connect each pair of farms, you must find the minimum amount of fiber needed to connect them all together. Each farm must connect to some other farm such that a packet can flow from any one farm to any other farm.
The distance between any two farms will not exceed 100,000.
Input
The input includes several cases. For each case, the first line contains the number of farms, N (3 <= N <= 100). The following lines contain the N x N conectivity matrix, where each element shows the distance from on farm to another. Logically, they are N lines of N space-separated integers. Physically, they are limited in length to 80 characters, so some lines continue onto others. Of course, the diagonal will be 0, since the distance from farm i to itself is not interesting for this problem.
Output
For each case, output a single integer length that is the sum of the minimum length of fiber required to connect the entire set of farms.
Sample Input
4 0 4 9 21 4 0 8 17 9 8 0 16 21 17 16 0
Sample Output
28
}写成while(cin>>n,n)尼玛就超时了,害我调试半天,有大神知道原因的嘛....啊啊啊啊~~while(cin>>n) { if(n==0)break;
#include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; struct edge{ int u,v,w; friend bool operator <(edge e1,edge e2){ return e1.w<e2.w;}}; int n,x,ans; int f[555]; vector<edge>edges; int find(int x) { return f[x]<0?x:f[x]=find(f[x]); } bool merge(int x,int y) { int rx=find(x); int ry=find(y); if(rx!=ry){ f[rx]+=f[ry]; f[ry]=rx; return true; } return false; } void kruscal() { sort(edges.begin(),edges.end()); int cnt=0; for(int i=0;i<edges.size();i++) { if(merge(edges[i].u,edges[i].v)){ ans+=edges[i].w; if(cnt==n-1)break; } } } int main() { while(cin>>n) { if(n==0)break; memset(f,-1,sizeof(f)); edges.clear();ans=0; for(int i=1;i<=n;i++) { for(int j=1;j<=n;j++) { cin>>x; if(j>i){ edges.push_back(edge{i,j,x}); } } } kruscal(); cout<<ans<<endl; } return 0; }
#include<iostream> #include<cstring> #include<algorithm> #include<vector> using namespace std; int n,x,ans; const int inf=0x3f3f3f3f; vector<int>g[555]; int map[555][555]; int book[555],dis[555]; void prim() { memset(book,0,sizeof(book)); memset(dis,0x3f,sizeof(dis)); dis[1]=0; while(1) { int index,minn=inf; for(int i=1;i<=n;i++) { if(!book[i]&&dis[i]<minn) { minn=dis[i]; index=i; } } if(minn==inf)break; ans+=dis[index]; book[index]=1; for(int i=0;i<g[index].size();i++) { if(dis[g[index][i]]>map[index][g[index][i]]) { dis[g[index][i]]=map[index][g[index][i]]; } } } } int main() { while(cin>>n) { if(n==0)break; ans=0;for(int i=0;i<=n;i++)g[i].clear(); for(int i=1;i<=n;i++) for(int j=1;j<=n;j++) { cin>>x; g[i].push_back(j); map[i][j]=x; } prim(); cout<<ans<<endl; } return 0; }