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 某省调查城镇交通状况,得到现有城镇道路统计表,表中列出 ...
随机推荐
- 【POJ 3714】 Raid
[题目链接] http://poj.org/problem?id=3714 [算法] 分治求平面最近点对 [代码] #include <algorithm> #include <bi ...
- JSP-Runoob:JSP Session
ylbtech-JSP-Runoob:JSP Session 1.返回顶部 1. JSP Session HTTP是无状态协议,这意味着每次客户端检索网页时,都要单独打开一个服务器连接,因此服务器不会 ...
- 《Typecript 入门教程》 1、类
类 使用class + 类名 即可定义一个类,一个类中通常有3个成员:属性.构造函数.方法: 在类内部引用属性或方法事使用this调用,它表示我们访问的是类的成员. 我们使用new构造了Greeter ...
- JavaScript--什么是函数
函数是完成某个特定功能的一组语句.如没有函数,完成任务可能需要五行.十行.甚至更多的代码.这时我们就可以把完成特定功能的代码块放到一个函数里,直接调用这个函数,就省重复输入大量代码的麻烦. 如何定义一 ...
- 涨知识-VI 基于TCP/UDP的应用层协议
基于TCP/UDP的应用层协议: 基于TCP: Telnet(Teletype over the Network, 网络电传),通过一个终端(terminal)登陆到网络 FTP(File Trans ...
- 原生js实现简单的焦点图效果
用到一些封装好的运动函数,主要是定时器 效果为图片和图片的描述定时自动更换 <!DOCTYPE html> <html> <head> <meta chars ...
- [转]STL之deque容器详解
Deque 容器 deque容器是C++标准模版库(STL,Standard Template Library)中的部分内容.deque容器类与vector类似,支持随机访问和快速插入删除,它在容器中 ...
- html5——3D转换
角度旋转 rotateX:默认以center绕x轴旋转 rotateY:默认以center绕y轴旋转 rotateZ:默认以cente绕z轴r旋转 //rotateX原点为center==>正值 ...
- HDU_1158_Employment Planning_dp
Employment Planning Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Othe ...
- C# textbox 获得焦点
this.ActiveControl = txt_core;