链接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 最短的更多相关文章

  1. ACM: HDU 2544 最短路-Dijkstra算法

    HDU 2544最短路 Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u Descrip ...

  2. hdu 2544

    #include <iostream> #include <cstdio> #define INF 9999999 //#define INF 0x3f3f3f3 using ...

  3. UESTC 30 &&HDU 2544最短路【Floyd求解裸题】

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...

  4. 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 ...

  5. hdu 2544 单源最短路问题 dijkstra+堆优化模板

    最短路 Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submis ...

  6. HDU - 2544最短路 (dijkstra算法)

    HDU - 2544最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shirt.但是每当我们的工作人员把上百件的衣服从商店运回到赛场的时候,却是非常累的!所以 ...

  7. (重刷)HDU 1874 畅通工程续 + HDU 2544 最短路 最短路水题,dijkstra解法。

    floyd解法 今天初看dijkstra,先拿这两题练手,其他变形题还是不是很懂. 模版题,纯练打字... HDU 1874: #include <cstdio> #define MAXN ...

  8. 单源最短路模板 + hdu - 2544

    Floyd Floyd 本质上类似一种动态规划,dp [ i ] [ j ] = dp [ i ] [ k ] + dp[ k ] [ j ]. /** * Night gathers, and no ...

  9. hdu 2544 最短路

    题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2544 最短路 Description 在每年的校赛里,所有进入决赛的同学都会获得一件很漂亮的t-shi ...

随机推荐

  1. 基于visual Studio2013解决C语言竞赛题之0202坐标转换

    题目

  2. 解题报告 HDU1944 S-Nim

    S-Nim Time Limit: 5000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others) Problem De ...

  3. Android apk逆向实战

    简介 逆向Android apk其实是一个分析Android apk的一个过程,必须了解Android程序开发的流程.结构.语句分支.解密原理等等. 功能 破解一个注册验证程序(自写一个简单的注册验证 ...

  4. uva 10599 - Robots(II) (dp | 记忆化搜索)

    本文出自   http://blog.csdn.net/shuangde800 ------------------------------------------------------------ ...

  5. UVA 1619 Feel Good(DP)

    Bill is developing a new mathematical theory for human emotions. His recent investigations are dedic ...

  6. K - K.Bro Sorting

    Description Matt’s friend K.Bro is an ACMer. Yesterday, K.Bro learnt an algorithm: Bubble sort. Bubb ...

  7. 统计图表类库--libchart使用简介

    1.饼图 #载入类文件 include "../libchart/classes/libchart.php"; header("Content-type: image/p ...

  8. JVM -- CMS

    并发的标记—清除(Concurrent Mark Sweep,缩写为 CMS)收集器,使得在整个收集的过程中只是很短的暂停应用的执行,可通过在 JVM 参数中设置-XX:UseConcMarkSwee ...

  9. CF(435D - Special Grid)dp

    题目链接:http://codeforces.com/problemset/problem/435/D 题意:求三角形个数,三个点必须的白点上,而且三条边必须是横线,竖线或对角线,三条边上不同意出现黑 ...

  10. ARM标准汇编与GNU汇编

    ARM标准汇编与GNU汇编 http://www.cnblogs.com/hnrainll/archive/2011/05/17/2048315.html