HDU 1836 畅通工程
畅通工程
This problem will be judged on HDU. Original ID: 1863
64-bit integer IO format: %I64d Java class name: Main
Input
行对应村庄间道路的成本,每行给出一对正整数,分别是两个村庄的编号,以及此两村庄间道路的成本(也是正整数)。为简单起见,村庄从1到M编号。当N为0时,全部输入结束,相应的结果不要输出。
Output
Sample Input
3 3
1 2 1
1 3 2
2 3 4
1 3
2 3 2
0 100
Sample Output
3
?
Source
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
int mp[][],m,n,d[];
priority_queue< pii,vector< pii >,greater< pii > >q;
bool done[];
void prim(){
while(!q.empty()) q.pop();
for(int i = ; i <= n; i++){
d[i] = mp[][i];
done[i] = false;
q.push(make_pair(d[i],i));
}
bool flag = true;
int ans = ;
done[] = true;
while(!q.empty()){
int u = q.top().second;
int w = q.top().first;
q.pop();
if(done[u]) continue;
done[u] = true;
if(w >= INF){flag = false;break;}
ans += w;
for(int i = ; i <= n; i++){
if(d[i] > mp[u][i]){
d[i] = mp[u][i];
q.push(make_pair(d[i],i));
}
}
}
if(flag) printf("%d\n",ans);
else puts("?");
}
int main() {
int i,j,u,v,w;
while(scanf("%d",&m),m){
scanf("%d",&n);
for(i = ; i <= n; i++){
for(j = ; j <= n; j++)
mp[i][j] = INF;
}
for(i = ; i < m; i++){
scanf("%d %d %d",&u,&v,&w);
mp[u][v] = mp[v][u] = w;
}
prim();
}
return ;
}
HDU 1836 畅通工程的更多相关文章
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题
参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上: 迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...
- HDU 1875 畅通工程再续 (prim最小生成树)
B - 畅通工程再续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit S ...
- hdu 1874 畅通工程续
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=1874 畅通工程续 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过 ...
- hdu 1874 畅通工程续 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...
- HDU 1232(畅通工程)题解
以防万一,题目原文和链接均附在文末.那么先是题目分析: [一句话题意] 给定一具有N个节点的图和其边集,求其集合数量. [题目分析] 并查集经典题...其实就是创建好并查集就行了.. [算法流程] 于 ...
- <hdu - 1863> 畅通工程 并查集和最小生成树问题
本题链接:http://acm.hdu.edu.cn/showproblem.php?pid=1863 Problem Description: 省政府“畅通工程”的目标是使全省任何两个村庄间都可以 ...
- HDU 1874 畅通工程续【Floyd算法实现】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- HDU 1232 畅通工程(模板——并查集)
题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1232 Problem Description 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出 ...
随机推荐
- 重装mysql
重装mysql方法. 转自http://blog.sina.com.cn/s/blog_73000beb01012eh4.html 1.删除 mysql 1.1 sudo apt-get autore ...
- linux下的so、o、lo、a、la文件的区别
o: 编译的目标文件a: 静态库,其实就是把若干o文件打了个包so: 动态链接库(共享库) lo: 使用libtool编译出的目标文件,其实就是在o文件中添加了一些信息la: 使用libtool编译出 ...
- Java数组去重(利用数组,不借助集合)
今天有个同学问我说老师,Java里边数组怎么去重,在不借助List集合的情况下,最后呢我整理了一下,打算发一篇博文,希望能帮助到有用的人,大佬绕过 public static void arrUniq ...
- Android内存管理(12)*「实例」用Monitor 生成.hprof文件 并分析内存泄漏
参考 http://blog.csdn.net/xiaanming/article/details/42396507 基本步骤: 1,准备一个有内存泄漏的代码 2,如何发现内存泄漏 3,生成.hpro ...
- Elasticsearch之CURL命令的UPDATE
对于,Elasticsearch之CURL命令的UPDATE包括局部更新和全部更新.可以去看我写的另一篇博客. Elasticsearch之更新(全部更新和局部更新) 总结: ES全部更新,使用PUT ...
- Spring boot中的定时任务(计划任务)
从Spring3.1开始,计划任务在Spring中实现变得异常的简单.首先通过配置类注解@EnableScheduling来开启对计划任务的支持,然后再要执行的计划任务的方法上注释@Scheduled ...
- jquery ajax在IE9以下进行跨域请求时无效的问题
第一步:设置浏览器安全属性,启用[通过域访问数据源]选项: 1.选择Internet选项 2.选择安全---自定义级别 3.找到其他---通过域访问数据源,选择启用,然后确定就可以了. 第二步:调用a ...
- SQL 循环插入10000条
SQL> create table tt_test ( x int, y char(50) ); Table created. SQL> SQL> begin 2 for i in ...
- CSS——盒子
CSS中的盒子具有以下几个种重要的属性: 1.border(边框) :盒子的厚度 2.padding(内边距):盒子内容距离盒子边框的距离 3.margin(外边距):盒子边框与其他的盒子的距离
- C#——数据库的访问
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.T ...