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 ...
随机推荐
- Android studio dabao
首先肯定是配置gradle,百度一下就知道了,我的是mac下配置的,sudo vim ~/.bash_profile ,然后设置环境变量 GRADE_HOME=/Users/Admin/gradle; ...
- Pagodas(等差数列)
Pagodas Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Sub ...
- 【IPC进程间通信之四】数据复制消息WM_COPYDATA
IPC进程间通信+数据复制消息WM_COPYDATA IPC(Inter-Process Communication,进程间通信). 数据复制消息WM_C ...
- iOS开发中地图开发的简单应用
iOS上使用地图比Android要方便,只需要新建一个MKMapView,addSubView即可.这次要实现的效果如下: 有标注(大头针),定位,地图. 1.添加地图 1.1 新一个Single V ...
- BZOJ 1597: [Usaco2008 Mar]土地购买( dp + 斜率优化 )
既然每块都要买, 那么一块土地被另一块包含就可以不考虑. 先按长排序, 去掉不考虑的土地, 剩下的土地长x递增, 宽y递减 dp(v) = min{ dp(p)+xv*yp+1 } 假设dp(v)由i ...
- PHP学习笔记4-类/命名空间/成员方法/类方法
命名空间 namespace 类 class创建文件Hello.php,namespace是jikexueyuan: <?php /** * Created by PhpStorm. * U ...
- redis(四)redis与Mybatis的无缝整合让MyBatis透明的管理缓存
redis的安装 http://liuyieyer.iteye.com/blog/2078093 redis的主从高可用 http://liuyieyer.iteye.com/blog/207809 ...
- Pison geeker
Pison on scriptogr.am Pison Abraham Lincoln: "Nearly all men can stand adversity, but if you wa ...
- 查看电脑已安装的Jdk的位数
查看自己电脑已安装的Jdk的位数的方法: public class ShowJdkBit { public static void main(String[] args) { String arch ...
- BNU 26579 Andrew the Ant 【蚂蚁】
链接: http://www.bnuoj.com/bnuoj/problem_show.php?pid=26579 http://www.bnuoj.com/bnuoj/contest_show.ph ...