HDU 2544 最短
链接:http://acm.hdu.edu.cn/showproblem.php?
pid=2544
解析:
首先数据量为V<=100
那么这里使用不论什么基础的最短路的算法都不会超时!
常见数据:
5 6
1 2 10
1 3 4
2 4 2
3 4 3
2 5 5
4 5 100
// 答案:14 2 4
1 2 10
1 3 4
2 4 2
3 4 3
// 答案:9
Bellman-Ford算法:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define MAX_E 10010
#define MAX_V 105
#define INF 1e8 struct edge{int from, to, cost; };
edge es[2*MAX_E];
int d[MAX_V];
int V, E; void shortest_path(int s){
for(int i=1; i<=V; ++i) d[i] = INF;
d[s] = 0;
while(true){
bool update = false;
for(int i=0; i<2*E; ++i){
edge e = es[i];
if(d[e.from] != INF&&d[e.to]>d[e.from]+e.cost){
d[e.to] = d[e.from] + e.cost;
update = true;
}
}
if(!update) break; }
} int main(){
while(~scanf("%d%d", &V, &E), V||E){
int i;
int a,b,c; for(i=0; i<E; ++i){
scanf("%d%d%d", &a, &b, &c);
es[i].from = a; es[i].to = b; es[i].cost = c;
es[i+E].from = b; es[i+E].to = a; es[i+E].cost = c;
}
shortest_path(1);
printf("%d\n", d[V]);
}
}
Dijkstra算法:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
using namespace std;
#define MAX_V 105
#define MAX_X 105
#define INF 1e8 int cost[MAX_V][MAX_V];
int d[MAX_X];
bool used[MAX_X];
int n,m; void dijkstra(int s){
for(int i=1; i<=n; ++i){
d[i] = INF;
used[i] = false;
}
d[s] = 0; while(true){
int v = -1;
for(int u=1; u<=n; ++u){
if(!used[u]&&(v==-1||d[u]<d[v])) v = u;
}
if(v == -1) break;
used[v] = true;
for(int u=1; u<=n; ++u){
d[u] = min(d[u], d[v]+cost[v][u]);
}
}
} int main(){
while(~scanf("%d%d", &n, &m), m||n){
int i;
int a,b,c; for(i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
cost[i][j] = INF; for(i=0; i<m; ++i){
scanf("%d%d%d", &a, &b, &c);
cost[a][b] = c;
cost[b][a] = c;
}
dijkstra(1);
printf("%d\n", d[n]);
}
}
Floyd算法:
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <algorithm>
#include <iostream>
#include <cstring>
#include <string>
#include <vector>
#include <queue>
using namespace std;
#define MAX_V 105
#define MAX_X 105
#define INF 1e8 int d[MAX_V][MAX_V];
bool used[MAX_X];
int n,m; void floyd(){
for(int k=1; k<=n; ++k){
for(int i=1; i<=n; ++i){
for(int j=1; j<=n; ++j){
d[i][j] = min(d[i][j], d[i][k]+d[k][j]);
}
}
}
} int main(){
while(~scanf("%d%d", &n, &m), m||n){
int i;
int a,b,c; for(i=1; i<=n; ++i)
for(int j=1; j<=n; ++j)
d[i][j] = INF;
for(i=1; i<=n; ++i) d[i][i] = 0; for(i=0; i<m; ++i){
scanf("%d%d%d", &a, &b, &c);
d[a][b] = c;
d[b][a] = c;
}
floyd();
printf("%d\n", d[1][n]);
}
}
版权声明:本文博客原创文章。博客,未经同意,不得转载。
HDU 2544 最短的更多相关文章
- ACM: HDU 2544 最短路-Dijkstra算法
HDU 2544最短路 Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Descrip ...
- hdu 2544
#include <iostream> #include <cstdio> #define INF 9999999 //#define INF 0x3f3f3f3 using ...
- UESTC 30 &&HDU 2544最短路【Floyd求解裸题】
最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...
- 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最短路 (dijkstra算法)
HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...
- (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。
floyd解法 今天初看dijkstra,先拿这两题练手,其他变形题还是不是很懂. 模版题,纯练打字... HDU 1874: #include <cstdio> #define MAXN ...
- 单源最短路模板 + hdu - 2544
Floyd Floyd 本质上类似一种动态规划,dp [ i ] [ j ] = dp [ i ] [ k ] + dp[ k ] [ j ]. /** * Night gathers, and no ...
- hdu 2544 最短路
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...
随机推荐
- 移植FreeModbus+ModbusMaster+STM32至RT-Thread(3、4阶段)
一.简介及进展 经过一个多月的努力,目前项目开发已进入最后阶段.虽然比预期时间有些延迟,但也收获不少,边工作边开源的效率确实还有待提高. 简单说下目前的进展吧 1.目前项目已经在Github中开源,大 ...
- extjs 优化小建议
1 原文信息 原文标题: Sencha Con 2013: Ext JS Performance tips 原文地址: [http://edspencer.net/2013/07/19/sencha- ...
- css 基础(一)
一.css样式表的分类 首先介绍一下css中的样式表 a.外部样式表 将需要的样式放在单独的外部文件中,需要使用是直接调用,通常放在.css文件中.例如:/*以下部分是放在(my.css)自定义名 ...
- Win10玩魔兽争霸不能全屏显示的设置教程
Win10玩魔兽争霸不能全屏显示的设置教程: 1.右击桌面左下角的windows图标,显示图片内容,然后点击运行: 2.点击运行,有个输入框,输入regedit,点击确认: 3.显示win10注册表, ...
- Appium 服务命令行参数
Appium 可以直接通过命令行启动,同样支持命令行参数配置 参考: http://appium.io/slate/cn/master/ 使用方法 例如: appium --shell 服务器标志 ...
- python中enumerate 函数
enumerate 函数用于遍历序列中的元素以及它们的下标: >>> for i,j in enumerate(('a','b','c')): print i,j 0 a 1 b ...
- jq商品展示图放大镜 and 原生js和html5写的放大镜效果 ~~效果不错
<!DOCTYPE HTML><html lang="en-US"><head> <meta charset="UTF-8&qu ...
- redis(三)redis+Keepalived主从热备秒级切换
一 简介 安装使用centos 5.10 Master 192.168.235.135 Slave 192.168.235.152 Vip 192.168.235.200 编译环境 yum -y in ...
- SilkTest天龙八部系列5-类的属性
SilkTest的面向对象机制让用户可以为类定义属性,用property语句实现.除此以外用户在类中还可以定义成员变量和不可变的setting属性.也就是是说Silktest类中可以有以下三种属性/变 ...
- UVALive 5797 In Braille
题目 比赛总结 题意:给出1-9的盲文,每种盲文都是2×3的点阵,有些点是凸起的用*表示,其余的用.表示.要进行两种操作,1 把盲文变成数字,2 把数字变成盲文 解法:按规则模拟....注意读入的每个 ...