HDU 1874 畅通工程续
来源:互联网 发布:淘宝客服接待人数 编辑:程序博客网 时间:2024/06/02 02:13
畅通工程续
Time Limit: 3000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 36570 Accepted Submission(s): 13439
Problem Description
某省自从实行了很多年的畅通工程计划后,终于修建了很多路。不过路多了也不好,每次要从一个城镇到另一个城镇时,都有许多种道路方案可以选择,而某些方案要比另一些方案行走的距离要短很多。这让行人很困扰。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
现在,已知起点和终点,请你计算出要从起点到终点,最短需要行走多少距离。
Input
本题目包含多组数据,请处理到文件结束。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
每组数据第一行包含两个正整数N和M(0<N<200,0<M<1000),分别代表现有城镇的数目和已修建的道路的数目。城镇分别以0~N-1编号。
接下来是M行道路信息。每一行有三个整数A,B,X(0<=A,B<N,A!=B,0<X<10000),表示城镇A和城镇B之间有一条长度为X的双向道路。
再接下一行有两个整数S,T(0<=S,T<N),分别代表起点和终点。
Output
对于每组数据,请在一行里输出最短需要行走的距离。如果不存在从S到T的路线,就输出-1.
Sample Input
3 30 1 10 2 31 2 10 23 10 1 11 2
Sample Output
2-1
Author
linle
Source
2008浙大研究生复试热身赛(2)——全真模拟
最短路径入门题,想借此题把最短路的四种算法都敲一遍。
先是Floyd(46ms)
#include <stdio.h>#include <string.h>#include <algorithm>#define INF 0x3f3f3f3f#define N 205using namespace std;int graph[N][N];int main(){ int n,m; int s,t; while(scanf("%d%d",&n,&m)>0) { memset(graph,INF,sizeof(graph)); int a,b,x; while(m--) { scanf("%d%d%d",&a,&b,&x); graph[a][b] = graph[b][a] = min(graph[a][b],x); } scanf("%d%d",&s,&t); if(s!=t) { for(int k = 0;k < n;k++) for(int i = 0;i < n;i++) for(int j = 0;j < n;j++) graph[i][j]=min(graph[i][k]+graph[j][k],graph[i][j]); if(graph[s][t]!=INF) printf("%d\n",graph[s][t]); else printf("-1\n"); } else printf("0\n"); } return 0;}
dijkstra(0ms)
#include <stdio.h>#include <string.h>#include <algorithm>#include <queue>#define N 205#define M 2005#define INF 0x3f3f3f3fusing namespace std;int v[M],nxt[M],w[M];int first[N],d[N],e;void add_edge(int a,int b,int c){ v[e] = b; nxt[e] = first[a]; w[e] = c; first[a] = e++;}int main(){ int n,m; while(scanf("%d%d",&n,&m)>0) { e = 0; memset(first,-1,sizeof(first)); int a,b,x; while(m--) { scanf("%d%d%d",&a,&b,&x); add_edge(a,b,x); add_edge(b,a,x); } int s,t; scanf("%d%d",&s,&t); priority_queue <pair<int,int>,vector<pair<int,int>>,greater<pair<int,int>>> q; memset(d,-1,sizeof(d)); d[s] = 0; q.push(make_pair(0,s)); while(!q.empty()) { while(!q.empty() && q.top().first > d[q.top().second]) q.pop(); if(q.empty()) break; int u = q.top().second; q.pop(); for(int i = first[u];i != -1;i = nxt[i]) { if(d[v[i]] == -1 || d[v[i]] > d[u]+w[i]) { d[v[i]] = d[u]+w[i]; q.push(make_pair(d[v[i]],v[i])); } } } printf("%d\n",d[t]); } return 0;}
0 0
- HDU 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- hdu 1874 畅通工程续
- HDU 1874 畅通工程续
- hdu 1874 畅通工程续
- HDU 1874 畅通工程续
- HDU 1874 畅通工程续
- hdu 1874(畅通工程续)
- Python 中的继承之Super用法
- ExtJS 常用组件属性 及使用方法
- SDUT 2413 哈希
- 语不惊人死不休(157)自由选择,自负后果
- 在AndroidMainfest中锁定屏幕方向
- HDU 1874 畅通工程续
- KVC基本使用
- linux ln 命令使用参数详解(ln -s 软链接)
- position与anchorPoint
- 常见排序算法(冒泡,选择,快速)的C语言实现【修正版】
- Gonet2 游戏服务器框架解析之gRPC提高(5)
- HDU 2255 奔小康赚大钱 (KM算法 模板)
- 九度oj 1066
- python爬队员oj题数