BNUOJ 6023 畅通工程续
畅通工程续
This problem will be judged on HDU. Original ID: 1874
64-bit integer IO format: %I64d Java class name: Main
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
Sample Input
3 3
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
Sample Output
2
-1
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;
const int maxn = ;
int n,m,mp[maxn][maxn],d[maxn],s,t;
bool done[maxn];
priority_queue< pii,vector< pii > ,greater< pii > >q;
void dijkstra(int s){
while(!q.empty()) q.pop();
int i,j,u,v;
for(i = ; i < n; i++){
done[i] = false;
d[i] = INF;
}
d[s] = ;
q.push(make_pair(d[s],s));
while(!q.empty()){
u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(v = ; v < n; v++){
if(d[u] < INF && mp[u][v] < INF && d[v] > d[u]+mp[u][v]){
d[v] = d[u]+mp[u][v];
q.push(make_pair(d[v],v));
}
}
if(done[t]) return;
}
}
int main() {
int i,j,u,v,w;
while(~scanf("%d %d",&n,&m)){
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);
if(mp[u][v] > w) mp[u][v] = mp[v][u] = w;
}
scanf("%d %d",&s,&t);
dijkstra(s);
printf("%d\n",d[t] == INF?-:d[t]);
}
return ;
}
#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 INF 0x3f3f3f3f
using namespace std;
const int maxn = ;
int n,m,s,t,d[maxn],mp[maxn][maxn];
bool done[maxn];
void spfa(int s){
int i,j,u,v;
queue<int>q;
for(i = ; i < n; i++)
d[i] = INF;
memset(done,false,sizeof(done));
d[s] = ;
q.push(s);
done[s] = true;
while(!q.empty()){
u = q.front();
q.pop();
done[u] = false;
for(v = ; v < n; v++){
if(d[v] > d[u]+mp[u][v]){
d[v] = d[u]+mp[u][v];
if(!done[v]){
done[v] = true;
q.push(v);
}
}
}
}
}
int main(){
int i,j,u,v,w;
while(~scanf("%d %d",&n,&m)){
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);
if(mp[u][v] > w) mp[u][v] = mp[v][u] = w;
}
scanf("%d %d",&s,&t);
spfa(s);
printf("%d\n",d[t] == INF?-:d[t]);
}
return ;
}
BNUOJ 6023 畅通工程续的更多相关文章
- 畅通工程续——E
E. 畅通工程续 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让 ...
- HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)
题目链接: 传送门 畅通工程续 Time Limit: 1000MS Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- 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 ...
- hdoj 1874 畅通工程续【dijkstra算法or spfa算法】
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
- 畅通工程续 HDOJ--1874
畅通工程续 Time Limit : 3000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submiss ...
- HDU-1874 畅通工程续 (最短路径启蒙题)
hdu 1874比较基础,拿来练各种刚学会的算法比较好,可以避免好多陷阱,典型的最短路模板题 畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memor ...
- 畅通工程续--hdu1874
畅通工程续 Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submi ...
随机推荐
- 51nod 1116 K进制下的大数
你万万想不到,Long Long 就能存下的数据 #include <iostream> #include <cstdio> #include <cstdlib> ...
- Kuskal/Prim POJ 1789 Truck History
题目传送门 题意:给出n个长度为7的字符串,一个字符串到另一个的距离为不同的字符数,问所有连通的最小代价是多少 分析:Kuskal/Prim: 先用并查集做,简单好写,然而效率并不高,稠密图应该用Pr ...
- Latex排版工具的使用(一) 分类: Latex 2014-06-14 22:52 448人阅读 评论(0) 收藏
使用Latex可以排版出漂亮的论文,尤其适合对含有数学公式论文的排版. 下面编写第一Latex源文件,实现对两个数学公式的排版: 新建文件first.tex: \documentclass{artic ...
- IOS - PDF合并 - 转
来自:http://www.cnblogs.com/tx8899/p/4082749.html #pragma mark - Merge PDF - (void)mergePDF { NSArray ...
- 189 Rotate Array 旋转数组
将包含 n 个元素的数组向右旋转 k 步.例如,如果 n = 7 , k = 3,给定数组 [1,2,3,4,5,6,7] ,向右旋转后的结果为 [5,6,7,1,2,3,4].注意:尽可能找 ...
- Java8-Lomda表达式
Lomda表达式 /** * All rights Reserved, Designed By www.bingo.com * @Title TestLamda.java * @author yang ...
- zTree树形控件讲解
由于截图时间距离有些长,找不到原文出处,如有侵权请联系删除.
- 4 Visual Effects 视觉效果 读书笔记 第四章
4 Visual Effects 视觉效果 读书笔记 第四章 Well, circles and ovals are good, but how about drawing r ...
- zabbix监控之grafana
zabbix监控之grafana
- (转)淘淘商城系列——发布dubbo服务
http://blog.csdn.net/yerenyuan_pku/article/details/72758639 Dubbo采用全Spring配置方式,透明化接入应用,对应用没有任何API侵入, ...