1030. Travel Plan
来源:互联网 发布:windows优化大师64 编辑:程序博客网 时间:2024/06/10 18:54
1030. Travel Plan (30)
A traveler's map gives the distances between cities along the highways, together with the cost of each highway. Now you are supposed to write a program to help a traveler to decide the shortest path between his/her starting city and the destination. If such a shortest path is not unique, you are supposed to output the one with the minimum cost, which is guaranteed to be unique.
Input Specification:
Each input file contains one test case. Each case starts with a line containing 4 positive integers N, M, S, and D, where N (<=500) is the number of cities (and hence the cities are numbered from 0 to N-1); M is the number of highways; S and D are the starting and the destination cities, respectively. Then M lines follow, each provides the information of a highway, in the format:
City1 City2 Distance Cost
where the numbers are all integers no more than 500, and are separated by a space.
Output Specification:
For each test case, print in one line the cities along the shortest path from the starting point to the destination, followed by the total distance and the total cost of the path. The numbers must be separated by a space and there must be no extra space at the end of output.
Sample Input4 5 0 30 1 1 201 3 2 300 3 4 100 2 2 202 3 1 20Sample Output
0 2 3 3 40
题目:
求最短路径,路径长度相等的时候取cost最小的路径
思路&&解法:
通用dijkstra解体模板,所有路径保存在vector《vector《int》》里面
Code:
#include <iostream>#include <string>#include <vector>#include <stdio.h>#include <algorithm>#include <math.h>#define MAXN 500#define UNREACHABLE 1000000int graph[MAXN][MAXN];int weight[MAXN][MAXN];int main() {int n, m, start_vex, des_vex;scanf("%d %d %d %d", &n, &m, &start_vex, &des_vex);if (n == 1) {printf("0 0 0");system("pause");return 0;}int d[MAXN];int w[MAXN];bool vis[MAXN];std::fill(vis, vis+MAXN, false);std::fill(w, w+MAXN, UNREACHABLE);std::fill(d, d+MAXN, UNREACHABLE);std::fill(graph[0], graph[0]+MAXN*MAXN, UNREACHABLE);std::fill(weight[0], weight[0] + MAXN*MAXN, UNREACHABLE);for (int i = 0; i < m; i++) {int start, end, distance, cost;scanf("%d %d %d %d", &start, &end, &distance, &cost);graph[start][end] = graph[end][start] = distance;weight[start][end] = weight[end][start] = cost;}d[start_vex] = 0;std::vector<int> route[MAXN];std::vector<std::vector<int>> all_route[MAXN];for (int i = 0; i < n; i++) {all_route[i].push_back(route[i]);}for (int i = 0; i < n; i++) {int find_x = -1;int max_d = UNREACHABLE;for (int j = 0; j < m; j++) {if (!vis[j] && d[j] < max_d) {max_d = d[j];find_x = j;}}vis[find_x] = true;if (find_x == -1) {continue;}route[find_x].push_back(find_x);for (int j = 0; j < all_route[find_x].size(); j++) {all_route[find_x][j].push_back(find_x);}for (int j = 0; j < n; j++) {if (!vis[j] && d[find_x] + graph[find_x][j] == d[j]) {all_route[j].push_back(route[find_x]);}else if (!vis[j] && d[find_x] + graph[find_x][j] < d[j]) {d[j] = d[find_x] + graph[find_x][j];route[j] = route[find_x];all_route[j] = all_route[find_x];}}}int mark;int total_weight;int min_weight = UNREACHABLE;for (int i = 0; i < all_route[des_vex].size(); i++) {total_weight = 0;for (int j = 0; j < all_route[des_vex][i].size() -1; j++) {total_weight += weight[all_route[des_vex][i][j]][all_route[des_vex][i][j+1]];}if (total_weight < min_weight) {min_weight = total_weight;mark = i;}}for (int i = 0; i < all_route[des_vex][mark].size(); i++) {printf("%d ", all_route[des_vex][mark][i]);}printf("%d %d", d[des_vex], min_weight);system("pause");}
- 1030. Travel Plan (30)
- 1030. Travel Plan
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan
- 1030. Travel Plan
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030.Travel Plan
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- 1030. Travel Plan (30)
- Android 使用Android Studio来生成SHA1以及MD5,还有包名
- Linux20个基础命令学习
- 王道计算机网络--2.1通信基础
- 关于 android listview 加载数据错位(错乱)问题
- java中int和integer比较,integer和integer比较
- 1030. Travel Plan
- 可靠传输之TCP协议
- spring cloud框架搭建
- java---Properties文件读写类
- Java源码分析之Arrays
- Oracle临时表空间综述
- SPI驱动:(Linux驱动7)
- JPA入门例子(采用JPA的hibernate实现版本)
- 多线程任务队列MulitiThreadJob实现