最短路 HDU 2544
最短路
Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 50686 Accepted Submission(s): 22280
入包括多组数据。每组数据第一行是两个整数N、M(N<=100,M<=10000),N表示成都的大街上有几个路口,标号为1的路口是商店
所在地,标号为N的路口是赛场所在地,M则表示在成都有几条路。N=M=0表示输入结束。接下来M行,每行包括3个整数A,B,C(1<=A,B&
lt;=N,1<=C<=1000),表示在路口A与路口B之间有一条路,我们的工作人员需要C分钟的时间走过这条路。
输入保证至少存在1条商店到赛场的路线。
1 2 3
3 3
1 2 5
2 3 5
3 1 2
0 0
2
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <queue>
#include <vector>
using namespace std;
const int INF = 0x3f3f3f3f;
struct edge{
int to,cost;
};
typedef pair<int,int> p;
int V;
vector<edge> G[];
int d[];
void dijkstra(int s){
priority_queue<p,vector<p>,greater<p> > q;
memset(d,INF,sizeof(d));
d[s] = ;
q.push(p(,s));
while(!q.empty()){
p cur = q.top(); q.pop();
int v = cur.second;
if(d[v]<cur.first) continue;
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(p(d[e.to],e.to));
}
}
}
}
void solve(){
int n,m;
while(scanf("%d%d",&n,&m) == &&n&&m){
for(int i = ; i<; i++) G[i].clear();
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});
}
dijkstra();
printf("%d\n",d[n]);
}
}
int main()
{
solve();
return ;
}
最短路 HDU 2544的更多相关文章
- 最短路 HDU - 2544 (dijkstra算法或Floyd算法)
Dijkstra解法: #include <stdio.h> #include <iostream> #include <cstring> #include < ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- 单源最短路模板 + hdu - 2544
Floyd Floyd 本质上类似一种动态规划,dp [ i ] [ j ] = dp [ i ] [ k ] + dp[ k ] [ j ]. /** * Night gathers, and no ...
- HDU - 2544最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。
floyd解法 今天初看dijkstra,先拿这两题练手,其他变形题还是不是很懂. 模版题,纯练打字... HDU 1874: #include <cstdio> #define MAXN ...
- hdu 2544 hdu 1874 poj 2387 Dijkstra 模板题
hdu 2544 求点1到点n的最短路 无向图 Sample Input2 1 //结点数 边数1 2 3 //u v w3 31 2 52 3 53 1 20 0 Sample Output32 ...
- hdu 2544 单源最短路问题 dijkstra+堆优化模板
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- hdu 2544
#include <iostream> #include <cstdio> #define INF 9999999 //#define INF 0x3f3f3f3 using ...
随机推荐
- 删除 mysql 日志文件后 ,启动出错
把 mysql-bin.index 里面的索引全部删除
- javascript 浏览器
hashchange事件 window.location.hash.slice(1) 添加和修改历史记录条目LINKHTML5引进了history.pushState()方法和history.repl ...
- 深入浅出Koa
深入浅出Koa(1):生成器和Thunk函数 Koa是个小而美的Node.js web框架,它由Express的原班人马打造的, 致力于以一种现代化开发的方式构建web应用. 通过这个系列,你将能够理 ...
- unable to connect to :5555
有可能批处理文件用的adb和eclipse的adb不兼容.把你的批处理文件用的adb换成eclipse的adb就可以了: 运行结果:
- 新版iTunes如何设置手机铃声
iTunes版本:12.5.1 系统版本:macOS Sierra 10.12 1.下载音乐,添加到iTunes. 现在下载音乐也不是一件容易的事,毕竟尊重版权. 这里Mac版与Windows版操作不 ...
- sql语句按月份统计查询
select year(createdate) 年,month(createdate) 月,count(1) from public_cms_arcwhere (userid in (select i ...
- Delphi XE2 生成的.exe 在未安装有Delphi的电脑上运行提示 “丢失 rtl160.bpl”
解决方案: XE2中加入了多平台的概念,默认的Release模式,也是带包编译,带运行时库的,所以,需要手工设置一下工程选项: 打开工程以后,Project-->Options-->左侧树 ...
- Java学习笔记之多态
1.父类型的引用可以指向子类型的对象: Parent p = new Child(); 2.当使用多态方式调用方法时,首先检查父类中是否有该方法,如果没有,则编译错误:如果有,再去调用子类的该同名方法 ...
- Beam me out!
Beam me out! 题目描述 King Remark, first of his name, is a benign ruler and every wrongdoer gets a second ...
- js播放器
<object?id="player"?height="64"?width="260"?classid="CLSID:6BF ...