[最短路径] HDU 1690 - Bus System
来源:互联网 发布:上海达观数据公司咋样 编辑:程序博客网 时间:2024/06/15 03:56
给定车辆的收费标准与路程区间的关系如下:
路程区间 花费 (0, L1] C1 (L1, L2] C2 (L2, L3] C3 (L3, L4] C4 (L4,+∞ ] 没票接着输入两个整数N, M。
接着输入N个车站所在的横坐标,这N个车站是在一条直线上的。
接下来有M个询问,Si, Ei分别代表起点和终点。
求起点到终点的最少花费?由于N的范围比较小, 可以把这些车站两两之间距离存储到邻接矩阵中,然后跑一下最短路径即可。
注意INF的初值要大一点。
#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <algorithm>#include <iostream>#include <set>#include <map>#include <queue>#include <stack>#include <assert.h>#include <time.h>typedef long long LL;const LL INF = 1e18;const double EPS = 1e-9;const double PI = acos(-1.0);using namespace std;LL dis[105], graph[105][105];int N, vis[105];void init(){ for(int i = 0; i <= 100; i++) { for(int j = 0; j <= 100; j++) { graph[i][j] = INF; } }}int abs(int x){ if(x < 0) return -x; return x;}void Dijkstra(int s){ for(int i = 1; i <= N; i++) { dis[i] = graph[s][i]; vis[i] = -1; } vis[s] = 0; for(int i = 2; i <= N; i++) { LL minn = INF, k = -1; for(int j = 1; j <= N; j++) { if(vis[j] == -1 && dis[j] < minn) { minn = dis[j]; k = j; } } if(k == -1) break; vis[k] = 0; for(int j = 1; j <= N; j++) { if(vis[j] == -1 && dis[j] > dis[k] + graph[k][j]) { dis[j] = dis[k] + graph[k][j]; } } }}int main(){ #ifdef _T1est freopen("test0.in", "r", stdin); freopen("test0.out", "w", stdout); srand(time(NULL)); #endif int T, L[10], C[10], M, val[105]; scanf("%d", &T); for(int i = 1; i <= T; i++) { printf("Case %d:\n", i); init(); for(int ii = 0; ii < 4; ii++) { scanf("%d", &L[ii]); } for(int ii = 0; ii < 4; ii++) { scanf("%d", &C[ii]); } scanf("%d %d", &N, &M); for(int ii = 1; ii <= N; ii++) { scanf("%d", &val[ii]); } for(int ii = 1; ii <= N; ii++) { for(int jj = 1; jj <= N; jj++) { if(ii != jj) { int tmp = abs(val[ii] - val[jj]); for(int k = 0; k < 4; k++) { if(tmp <= L[k]) { graph[ii][jj] = C[k]; break; } } } } } for(int ii = 0; ii < M; ii++) { int s, e; scanf("%d %d", &s, &e); Dijkstra(s); if(dis[e] == INF) { printf("Station %d and station %d are not attainable.", s, e); } else { printf("The minimum cost between station %d and station %d is %I64d.", s, e, dis[e]); } puts(""); } } return 0;}
0 0
- [最短路径] HDU 1690 - Bus System
- HDU 1690 Bus System (最短路径bellmamford算法)
- hdoj 1690 Bus System 【最短路径】
- (step6.2.1)hdu 1690(Bus System——最短路径)
- HDOJ 题目1690Bus System(最短路径)
- HDU 1690 Bus System(多源最短路径)
- HDU 1690 多源最短路径 Bus System
- hdu 1690 Bus System Floyd 求最短路径
- hdu 1690Bus System,(最短路)
- hdu 1690 Bus System(最短路)
- hdu 1690 Bus System(最短路)
- hdu 1690 Bus System(Dijkstra最短路)
- HDU 1690 Bus System
- HDU 1690 Bus System
- hdu 1690 Bus System
- HDU 1690 Bus System
- HDU 1690 Bus System
- hdu 1690 Bus System
- Android中attrs.xml文件的使用详解
- 突击Mercurial SCM(HG)8---hg server 用户认证
- php 多语言切换
- 相机
- Failed to execute goal org.codehaus.mojo:hibernate3-maven-plugin:2.1:hbm2ddl
- [最短路径] HDU 1690 - Bus System
- UIView视图中比较常见的方法总结
- 【黑马程序员】分类与协议
- leetcode-20 Valid Parentheses
- 读《项目经理应该知道的97件事》
- 如何决定 Web 应用的线程池大小
- 使用jstl c:when报500的教训
- LeetCode Combination Sum II
- Message: Access to the registry key 'Global' is denied.