HDU 2544 最短
2015-08-18 10:00
417 查看
链接:http://acm.hdu.edu.cn/showproblem.php?
pid=2544
解析:
首先数据量为V<=100
那么这里使用不论什么基础的最短路的算法都不会超时!
常见数据:
Bellman-Ford算法:
Dijkstra算法:
[b]Floyd算法:
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] = c; cost[b][a] = c; } dijkstra(1); printf("%d\n", d ); } }
[b]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] ); } }
相关文章推荐
- nginx基础应用
- HDU 3342 Legal or Not (拓扑排序)
- 稳定排序和不稳定排序
- qwt6.1.0编译与安装
- java 数组:java.lang.ClassCastException
- 线程同步与互斥:互斥锁
- R语言_图形初阶
- iOS 6.0之后支持一个页面横屏的方法
- CSS3展现精彩的动画效果 css3的动画属性
- 关于几个好的博客
- Apache性能监控
- 在Web 应用的生命周期中,ServletContext 对象最早被创建,最晚被销毁。
- Project Savanna:让Hadoop运行在OpenStack之上
- 找到 mysql 数据库中的不良索引
- 【学习】Hadoop、爬虫和Elasticsearch的企业级应用
- 低功耗蓝牙开发技术概述(二)——控制器(上)
- 1025. PAT Ranking
- Java 程序优化:字符串操作、基本运算方法等优化策略
- gradle依赖排除
- 所属分类: 深入理解Magento Magento后台表单添加层级表格