2436: Next Generation Network

本文探讨了一种将传统电话网络升级为互联网网络的方法,旨在使岛上的城市能够直接或间接地通过互联网进行通信。文章详细介绍了如何通过选择并升级部分现有电话线路来实现这一目标,同时确保整体成本最小。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2436: Next Generation Network


ResultTIME LimitMEMORY LimitRun TimesAC TimesJUDGE
5s32768K1186153Standard

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;
}

 

 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值