HDU - 3790最短路径问题经典模板题
2015-08-04 20:56
387 查看
HDU - 3790
最短路径问题
Submit Status
Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
Sample Output
最短路径问题
Time Limit: 1000MS | Memory Limit: 32768KB | 64bit IO Format: %I64d & %I64u |
Description
给你n个点,m条无向边,每条边都有长度d和花费p,给你起点s终点t,要求输出起点到终点的最短距离及其花费,如果最短距离有多条路线,则输出花费最少的。
Input
输入n,m,点的编号是1~n,然后是m行,每行4个数 a,b,d,p,表示a和b之间有一条边,且其长度为d,花费为p。最后一行是两个数 s,t;起点s,终点。n和m为0时输入结束。
(1<n<=1000, 0<m<100000, s != t)
Output
输出 一行有两个数, 最短距离及其花费。
Sample Input
3 2 1 2 5 6 2 3 4 5 1 3 0 0
Sample Output
9 11
/* Author: 2486 Memory: 3924 KB Time: 296 MS Language: G++ Result: Accepted */ #include <cstdio> #include <cstring> #include <algorithm> #include <vector> #include <queue> using namespace std; const int maxn = 1000 + 5; int d[maxn],v[maxn]; bool vis[maxn][maxn]; int n,m,s,t; typedef pair<int,int>P; struct edge { int to,cost,val; edge(int to,int cost,int val):to(to),cost(cost),val(val) {} }; vector<edge>G[maxn]; void init() { for(int i = 1; i <= n; i ++)G[i].clear(); memset(vis,false,sizeof(vis)); } void Dijkstra(int s) { priority_queue<P,vector<P>,greater<P> >que; memset(d,0x3f,sizeof(d)); memset(v,0,sizeof(v)); d[s] = 0; v[s] = 0; que.push(P(0,s)); while(!que.empty()) { P p = que.top(); que.pop(); int vs = p.second; if(d[vs] < p.first)continue; for(int i = 0; i < G[vs].size(); i ++) { edge e = G[vs][i]; if(d[e.to] > d[vs] + e.cost||(d[e.to] == d[vs] + e.cost && v[e.to] > v[vs] + e.val)) { d[e.to]=d[vs] + e.cost; v[e.to]=v[vs]+e.val; que.push(P(d[e.to],e.to)); } } } printf("%d %d\n",d[t],v[t]); } int main() { int a,b,d,p; while(~scanf("%d%d",&n,&m),n&&m) { init(); for(int i = 0; i < m; i ++) { scanf("%d%d%d%d",&a,&b,&d,&p); if(vis[a][b])continue; G[a].push_back(edge(b,d,p)); G[b].push_back(edge(a,d,p)); } scanf("%d%d",&s,&t); Dijkstra(s); } return 0; }
相关文章推荐
- android 如何获取当前系统所使用的语言
- HBuilder开发App教程09-侧滑菜单
- hdu 5348 MZL's endless loop
- 北京市海外学人中心《北京市留学人员工作居住证》攻略
- LeetCode#49 Anagrams
- Disque: antirez开发的开源分布式内存作业队列
- C++ 的Vector增长方式
- 记录一次服务器内存溢出
- 播放音乐并调节音量
- HBuilder开发App教程08-几种页面打开方式
- zoj 2100 (播种能否全部播完)(回溯算法的应用)
- 串结构练习——字符串连接
- 8-4
- Java通过Axis2发布WebService
- 循环语句输出菱形
- 3.PHP 教程_PHP 语法
- 2015 多校赛 第四场 1009 (hdu 5335)
- 将HttpServletRequest中的参数反射至实体类
- !HDU 4346 插旗子RGR的插法有多少种-思维&正难则逆&思维!
- HDU5351大数模板加斐波那契