HDU 1863 畅通工程

news/2024/6/15 11:25:45 标签: java, 测试

畅通工程

Time Limit: 1000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 28032    Accepted Submission(s): 12304

Problem Description
省政府“畅通工程”的目标是使全省任何两个村庄间都可以实现公路交通(但不一定有直接的公路相连,只要能间接通过公路可达即可)。经过调查评估,得到的统计表中列出了有可能建设公路的若干条道路的成本。现请你编写程序,计算出全省畅通需要的最低成本。
 
Input
测试输入包含若干测试用例。每个测试用例的第1行给出评估的道路条数 N、村庄数目M ( < 100 );随后的 N 行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
 
Output
对每个测试用例,在1行里输出全省畅通需要的最低成本。若统计数据不足以保证畅通,则输出“?”。
 
Sample Input
3 3 1 2 1 1 3 2 2 3 4 1 3 2 3 2 0 100
 
Sample Output
3 ?
 
Source
浙大计算机研究生复试上机考试-2007年
 
Recommend
lcy   |   We have carefully selected several similar problems for you:   1879  1232  1102  1272  1301 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 10010
int n,m,pre[maxn];
struct node
{
    int x,y,z;
}e[maxn];
bool cmp(node p,node q)
{
    return p.z<q.z;
}
int find(int x)
{
    int r = x;
    while(r!=pre[r])
      r = pre[r];
    int i=x,j;
    while(pre[i]!=r)
    {
        j = pre[i];
        pre[i] = r;
        i = j;
    }
    return r;
}
void kruskal()
{
    int num=0;
    int sum = 0;
    for(int i=0;i<n&&num!=m-1;i++)
    {
        int fx = find(e[i].x);
        int fy = find(e[i].y);
        if(fx!=fy)
        {
            sum += e[i].z;
            pre[fx] = pre[fy];
            num++;
        }
    }//加入边的数量等于m-1,则无向图连通,生成最小生成树 
    if(num<m-1)//如果加入边的数量小于m - 1,则表明该无向图不连通,等价于不存在最小生成树
       cout << "?" << endl; 
    else cout << sum << endl;
}
int main ()
{
    while(cin >> n >> m)
    {
        if(n==0)
           break;
        for(int i=0;i<m;i++)
           pre[i] = i;
        for(int i=0;i<n;i++)
        {
            cin >> e[i].x >> e[i].y >> e[i].z;
            e[i].x--,e[i].y--;
        }
        sort(e,e+n,cmp);
        kruskal();
    }
    return 0;
}

 

转载于:https://www.cnblogs.com/l609929321/p/6610418.html


http://www.niftyadmin.cn/n/1411817.html

相关文章

silverlight调用exe程序

一定要在项目中添加引用Microsoft.CSharp 这里是打开写字板程序 using System;using System.Collections.Generic;using System.Linq;using System.Net;using System.Runtime.InteropServices.Automation;using System.Windows;using System.Windows.Controls;using System.Wi…

Spring Cloud入门 (3) - REST客户端Feign

1、简介 在Spring Cloud集群中&#xff0c;各个角色的通信基于REST服务&#xff0c;因此在调用服务时&#xff0c;就不可避免的需要使用REST服务的请求客户端了。 Spring 中自带了RestTemplate&#xff0c;RestTemplate使用HttpClient发送请求。 Spring Cloud 将Feign 框架集成…

2017.3找工作面试记录-第一周(2)

接上一篇 2017.3找工作面试记录-第一周 2017.3找工作面试记录-第一周 2017.3找工作面试记录-第一周&#xff08;2&#xff09; 2017.3找工作面试记录-第二周 2017.4找工作面试记录-第三周 2017.4找工作面试记录-第三周(2)--金蝶 2017.4找工作面试记录-第三周(3) 4.一家做航空气…

浏览器安全标记

在silverlight网页常常看到一个标记 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns"http://www.w3.org/1999/xhtml" ><!-- saved fr…

安卓中百度地图的使用

安卓中百度地图的使用 1.导入到程序中 2.一些权限配置 <?xml version"1.0" encoding"utf-8"?> <manifest xmlns:android"http://schemas.android.com/apk/res/android"package"demo.jq.com.lbstest"><applicationand…

Spring Cloud入门 (4) - Spring Cloud保护机制

1、 Hystrix 运作流程 转载于:https://www.cnblogs.com/lkc9/p/11572062.html

一个很好用的sql在线美化器

功能很全,可以转T-sql,pl/sql,可以把它们转成C# StringBuilder,Java,Delphi,VB 语法的连接字符串 功能比PLSQL Developer的美化器更强 http://www.dpriver.com/pp/sqlformat.htm?refwangz.sqlformat.htm 希望能找到一个好的安装版本&#xff0c;在网页上反应速度不快啊

vuejs2.0子组件改变父组件的数据

在vue2.0之后的版本中&#xff0c;不允许子组件直接改变父组件的数据&#xff0c;在1.0的版本中可以这样操作的&#xff0c;但是往往项目需求需要改变父组件的数据&#xff0c;2.0也是可一个&#xff0c;区别是&#xff0c;当我们把父元素的数据给子组件时&#xff0c;需要传一…