2436: Next Generation Network
Result | TIME Limit | MEMORY Limit | Run Times | AC Times | JUDGE |
---|---|---|---|---|---|
![]() | 5s | 32768K | 1186 | 153 | Standard |
There are many citys in island A. In the old days, they communicate with each other using telephone. A telephone line connect two citys. There have existing some telephone lines.
The king of island A want his kingdom more modern. He prepare to construct an internet-network in his island, so every city can communicate with each other using internet-network directly or indirectly. But he is a stingy person. He only want to upgrade some existing telephone lines and cost the minimal money. Upgrade a telephone line must use half of its origin cost.
Input
The first line of each case has two integers: number of citys N(1<=N<=1000), number of existing telephone lines M(1<=M<=1000000). Following M lines each contain three integers u(1<=u<=N), v(1<=v<=N), w(0<=w<=1000). Means thers is a telephone line between city u and city v, its origin cost is w.
Output
Output the minimal upgrade cost if the king can achieve his aim. Otherwise output -1.
Sample Input
3 3 1 2 10 1 3 20 2 3 10 10 6 1 2 10 3 4 20 5 6 10 7 8 20 9 10 10 1 2 10
Sample Output
10 -1
Problem Source: provided by skywind
#include<stdio.h>
#include<string.h>
#define typec int
int c[1050][1050];
int n,m;
const int inf=(1<<31)-199;
int main()
{
int i,j,k;
while(scanf("%d%d",&n,&m)==2)
{
memset(c,12,sizeof(c));
while(m--)
{
int x,y,z;
scanf("%d%d%d",&x,&y,&z);
if(z<c[x-1][y-1])
c[x-1][y-1]=c[y-1][x-1]=z;
}
int vis[1050],lowc[1050];
int p;
typec minc, res = 0;
memset(vis, 0, sizeof(vis));
vis[0] = 1;
int flag=0;
for (i=1; i<n; i++) lowc[i] = c[0][i];
for (i=1; i<n; i++)
{
minc = inf; p = -1;
for (j=0; j<n; j++)
if (0 == vis[j] && minc > lowc[j]) {
minc = lowc[j]; p = j;
}
if (inf == minc) {flag=1; break;} // 原图不连通
res += minc; vis[p] = 1;
for (j=0; j<n; j++)
if (0 == vis[j] && lowc[j] > c[p][j])
lowc[j] = c[p][j];
}
if(flag) {printf("-1/n");continue;}
if(res%2==0) printf("%d/n",res/2);
else printf("%.1lf/n",double(res)/2);
}
return 0;
}