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 ...
随机推荐
- AseoZdpAseo.init(this, AseoZdpAseo.INSERT_TYPE);
让以后的人知道吧,这就是一个广告包,相当于广告插件.
- Mac OS X10.9安装的Python2.7升级Python3.3步骤详解
Mac OS X10.9默认带了Python2.7,不过现在Python3.3.3出来了,如果想使用最新版本,赶紧升级下吧.基本步骤如下 第1步:官网下载Python3.3 这里面有windows和m ...
- MAC Eclipse 快捷键
MAC Eclipse 快捷键大全备忘: Command + O:显示大纲 Command + 1:快速修复 Command + D:删除当前行 Command + Option + ↓:复制当前行到 ...
- php返回的json格式
public function search_ip(){ $where['ip'] = $_GET['ip']; $Machine = M('Machine_info'); $arr = $Machi ...
- C# 计算器 运算符和数字键的keys对照
keys. private void Computer_KeyDown(object sender, KeyEventArgs e) { if (e.KeyCode == Keys.NumPad0) ...
- CentOS 6.4安装(超级详细图解教程)
链接地址:http://www.osyunwei.com/archives/5855.html CentOS 6.4安装(超级详细图解教程) 附:CentOS 6.4下载地址 32位:http://m ...
- CentOS服务器下对mysql的优化
原文链接: CentOS服务器下对mysql的优化 一.mysql的优化思路 mysql的优化分为两方面: 1. 服务器使用前的优化 2. 服务使用中的优化 二.mysql的基础优化步骤 1. 硬件级 ...
- CPU满格的元凶,这回是由于QTimer引起的(默认interval是0,太猛)
timer_space = new QTimer(); qDebug() << SystemGlobal::m_app->SpaceUse; qDebug() << ti ...
- mysqli_set_charset和SET NAMES优劣分析
bool mysqli_set_charset ( mysqli $link , string $charset ) 这应该是首选的用于改变字符编码的方法,不建议使用 mysqli_query()执行 ...
- 基于visual Studio2013解决C语言竞赛题之0423比赛安排
题目