畅通工程续 (dijkstra)
畅通工程续
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 42384 Accepted Submission(s): 15689
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
每组数据第一行包含两个正整数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),分别代表起点和终点。
0 1 1
0 2 3
1 2 1
0 2
3 1
0 1 1
1 2
-1
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
const int INF = 0x3f3f3f3f;
struct edge{
int to,cost;
friend bool operator < (edge A,edge B){
return A.cost > B.cost;
}
};
int d[maxn];
vector<edge> g[maxn];
bool done[maxn];
void dijkstra(int s){
memset(d,INF,sizeof(d));
memset(done,false,sizeof(done));
d[s] = ;
priority_queue<edge> q;
q.push((edge){s,});
while(!q.empty()){
edge cur = q.top(); q.pop();
int v = cur.to;
if(done[v]) continue;
done[v] = true;
for(int i = ; i<g[v].size(); i++){
edge e = g[v][i];
if(d[e.to]>d[v]+e.cost){
d[e.to] = d[v]+e.cost;
q.push((edge){e.to,d[e.to]});
}
}
}
}
void solve(){
int n,m;
while(scanf("%d%d",&n,&m)!=EOF){
for(int i = ; i<m; i++){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
g[a].push_back((edge){b,c});
g[b].push_back((edge){a,c});
}
int s,t;
scanf("%d%d",&s,&t);
dijkstra(s);
if(d[t] == INF) printf("-1\n");
else printf("%d\n",d[t]);
for(int i = ; i<maxn; i++) g[i].clear();
}
}
int main()
{
solve();
return ;
}
畅通工程续 (dijkstra)的更多相关文章
- ACM: HDU 1874 畅通工程续-Dijkstra算法
HDU 1874 畅通工程续 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Desc ...
- hdu 1874 畅通工程续 Dijkstra
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1874 题目分析:输入起点和终点,顶点的个数,已连通的边. 输出起点到终点的最短路径,若不存在,输出-1 ...
- HDU 1874 畅通工程续-- Dijkstra算法详解 单源点最短路问题
参考 此题Dijkstra算法,一次AC.这个算法时间复杂度O(n2)附上该算法的演示图(来自维基百科): 附上: 迪科斯彻算法分解(优酷) problem link -> HDU 1874 ...
- hdu 1874 畅通工程续 (dijkstra(不能用于负环))
畅通工程续Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submis ...
- 畅通工程续(Dijkstra算法)
对Dijkstra算法不是很熟悉,写一下思路,希望通过写博客加深理解 Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时, ...
- hdu1874畅通工程续 (dijkstra)
Problem Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行 ...
- hdu 1874 畅通工程续(求最短距离,dijkstra,floyd)
题目:http://acm.hdu.edu.cn/showproblem.php?pid=1874 /************************************************* ...
- 畅通工程续——E
E. 畅通工程续 某省自从实行了很多年的畅通工程计划后,终于修建了很多路.不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多.这让 ...
- HDU 1874 畅通工程续(最短路/spfa Dijkstra 邻接矩阵+邻接表)
题目链接: 传送门 畅通工程续 Time Limit: 1000MS Memory Limit: 65536K Description 某省自从实行了很多年的畅通工程计划后,终于修建了很多路. ...
随机推荐
- 授权给指定用户,使用navicat在其他ip都可以连接linux服务器上的mysql库
grant all privileges on ao.* to 'tony'@'localhost' identified by '123456'; 在ao库中所有表. 同意 ,授权 给ton ...
- reincarnation server
- parent of all the drivers and servers - when a driver or server dies, RS collects it. - RS checks ...
- AFNetWorking 判断当前版本是否是最新版本
NSString *url = [[NSString alloc] initWithFormat:@"http://itunes.apple.com/lookup?id=%@",@ ...
- Postgres数据库在Linux中的I/O优化
I/O 优化1 打开 noatime方法: 修改 /etc/fstab2 调整预读方法: 查看 sudo blockdev --getra /dev/sda 设置 sudo blockdev --se ...
- network: 思科-华为光模块
思科-华为光模块的分类比较 摘要:本文介绍:思科GLC-SX-MM,GLC-LH-SM光模块等产品参数与图片,华为光模块的型号与分类等知识. 光模块分类与介绍 一.思科厂家 1.多模光模块 型号: ...
- 解决scrollview不滚动
scrollView不滚动的时候 试试这个,有时候药到病除:
- PHP基本类型操作
//关键字对大小写不敏感echo ('hello world!<br>');ECho ('hello world<br>');eCho ('hello world<br& ...
- asp中的动态数组
<% Dim array1(),i ReDim array1(3)array1(3)=10response.Write(array1(3)&"<br>") ...
- curl 测试web站点的响应时间
curl -s -w "\n"::%{time_namelookup}::%{time_connect}::%{time_starttransfer}::%{time_total} ...
- EL(表达式语言)
EL(Expression Language):目的是为了简化Jsp页面的语言,使页面看起来更加简洁 基本的语法特点 以“${"开头,以”}“结束 一 与低版本的环境兼容----禁用EL ( ...