PAT-A | 1003 | Emergency
来源:互联网 发布:如何制作淘宝店招图片 编辑:程序博客网 时间:2024/05/22 17:15
1003. Emergency (25)
As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.
Input
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (<= 500) - the number of cities (and the cities are numbered from 0 to N-1), M - the number of roads, C1 and C2 - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c1, c2 and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C1 to C2.
Output
For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, and the maximum amount of rescue teams you can possibly gather.
All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.
5 6 0 21 2 1 5 30 1 10 2 20 3 11 2 12 4 13 4 1Sample Output
2 4
以下是我自己的不全对的解法,就是把Dijkstra稍微改动一下,但是测试用例只通过了前两个,后面4个都没有通过。看了浙大的《数据结构学习与实验指导》6-8 城市间紧急救援的解答才明白问题出在最短路径条数的统计上(我自己设计了一个测试用例,也发现了这个问题,但是不知道怎么解决)。
#include <iostream>#define INF 65535using namespace std;const int MAXSIZE = 500;struct MGraph {//int Edge[MAXSIZE][MAXSIZE];int **Edge;int *teams;int N;int M;//number of edges//if no edge between a and b, Edge[a][b] = INF};void CreateGraph(MGraph &G, int &source, int &dest){int a, b, w;cin >> G.N >> G.M;cin >> source >> dest;G.Edge = new int*[G.N];G.teams = new int[G.N];//initialize the number of teams of each cityfor (int i = 0; i != G.N; ++i) {G.Edge[i] = new int[G.N];cin >> G.teams[i];for (int j = 0; j != G.N; ++j) {G.Edge[i][j] = INF;}}//initialize edge and weightfor (int i = 0; i != G.M; ++i) {cin >> a >> b >> w;G.Edge[a][b] = w;G.Edge[b][a] = w;}}//Dijkstra Alg//path[], dist[], set[]//return the number of same shortest pathesint Dijkstra(MGraph G, int s, int path[], int dist[], int sum[]){int *set = new int[G.N];int cnt = 1;//initializefor (int i = 0; i != G.N; ++i) {set[i] = 0;dist[i] = G.Edge[s][i];if (G.Edge[s][i] != INF) {path[i] = s;sum[i] = G.teams[s] + G.teams[i];} else {path[i] = -1;sum[i] = G.teams[i];}}set[s] = 1;path[s] = -1;for (int i = 0; i != G.N; ++i) {//find min and add to set[]int min = INF, v = 0;for (int j = 0; j != G.N; ++j) {if (set[j] == 0 && dist[j] < min) {min = dist[j];v = j;}}set[v] = 1;//refresh dist[]for (int j = 0; j != G.N; ++j) {if (G.Edge[v][j] != INF && set[j] == 0) {if (dist[v] + G.Edge[v][j] < dist[j]) {dist[j] = dist[v] + G.Edge[v][j];sum[j] = sum[v] + G.teams[j];path[j] = v;} else if (dist[v] + G.Edge[v][j] == dist[j]) {++cnt;if (sum[v] + G.teams[j] > sum[j]) {sum[j] = sum[v] + G.teams[j];}path[j] = v;}}}}delete[]set;return cnt;}void PrintPath(int path[], int a){int stack[MAXSIZE], top = -1;while (path[a] != -1) {stack[++top] = a;a = path[a];}stack[++top] = a;while (top != -1) {cout << stack[top--] << " ";}}int main(){MGraph G;int *path, *dist, *sum;int source, dest, cnt;CreateGraph(G, source, dest);path = new int[G.N];dist = new int[G.N];sum = new int[G.N];cnt = Dijkstra(G, source, path, dist, sum);cout << cnt << " " << sum[dest] << endl;//PrintPath(path, dest);//remember to free G's memorydelete[]path;delete[]dist;}
解题要点:
这道题的关键是如何统计最短路径的条数:
需要记录从S到每个结点的最短路的条数,这些记录初始化为0,S的记录初始化为1。每当发现一个结点的新的更短路径时,其“最短路径条数”等同于该路径上前驱结点的“最短路径条数”(注意:这里不是更新为1);而发现等长最短路径时,其“最短路径条数”应该加上该路径上前驱结点的“最短路径条数”(注意:这里不是当前路径条数加1)
实现要点:
输入有可能是完全图,所以用邻接矩阵表示比较方便。
需要存储边的距离edge[][]和救援队数量teams[],在结构体中增加sum[],记录经过i结点可以集结的救援队数量;用数组count[]记录每个结点最短路径的条数;用数组pre[]记录路径中每个结点的前驱结点。当最短路径需要更新的时候,不仅要更新最短路径长度dist[],还需要更新sum[],count[]和pre[]
#include <iostream>#define INF 65535using namespace std;struct MGraph {int **Edge;//if no edge between a and b, Edge[a][b] = INFint *teams;// teams[] record the number of rescue teams in the i-th cityint *dist;// dist[] record the shortest distance from source to i-th vertex(city)int *sum;// sum[] record the biggest sum of rescue teams from source to i-th cityint *count;// record the number of shortest pathes from source to i-th vertexint *pre;// record the previous vertex of i-th vertex in the shortest path, its function like path[]int N;// int M;//number of edges};void CreateGraph(MGraph &G, int &source, int &dest){int a, b, w;cin >> G.N >> G.M;cin >> source >> dest;G.Edge = new int*[G.N];G.teams = new int[G.N];G.dist = new int[G.N];G.sum = new int[G.N];G.count = new int[G.N];G.pre = new int[G.N];//initialize the number of teams of each cityfor (int i = 0; i != G.N; ++i) {G.Edge[i] = new int[G.N];cin >> G.teams[i];for (int j = 0; j != G.N; ++j) {G.Edge[i][j] = INF;}}//initialize edge and weightfor (int i = 0; i != G.M; ++i) {cin >> a >> b >> w;G.Edge[a][b] = w;G.Edge[b][a] = w;}}//Dijkstra Algvoid Dijkstra(MGraph G, int s){int *set = new int[G.N];//initializeG.count[s] = 1;for (int i = 0; i != G.N; ++i) {set[i] = 0;G.dist[i] = G.Edge[s][i];if (G.Edge[s][i] != INF) {G.pre[i] = s;G.count[i] = G.count[s];G.sum[i] = G.teams[s] + G.teams[i];} else {G.pre[i] = -1;//ps this statement: if i == 0, G.count[0] = 0, then G.count[s] = 1 is invalidif (i != 0) G.count[i] = 0;G.sum[i] = G.teams[i];}}set[s] = 1;G.count[s] = 1;G.pre[s] = -1;//key operationfor (int i = 0; i != G.N; ++i) {//find min and add to set[]int min = INF, v = 0;for (int j = 0; j != G.N; ++j) {if (set[j] == 0 && G.dist[j] < min) {min = G.dist[j];v = j;}}set[v] = 1;//refresh dist[]for (int j = 0; j != G.N; ++j) {if (G.Edge[v][j] != INF && set[j] == 0) {if (G.dist[v] + G.Edge[v][j] < G.dist[j]) {G.dist[j] = G.dist[v] + G.Edge[v][j];G.sum[j] = G.sum[v] + G.teams[j];G.pre[j] = v;G.count[j] = G.count[G.pre[j]];} else if (G.dist[v] + G.Edge[v][j] == G.dist[j]) {if (G.sum[v] + G.teams[j] > G.sum[j]) {G.sum[j] = G.sum[v] + G.teams[j];}G.pre[j] = v;G.count[j] += G.count[G.pre[j]];}}}}delete[]set;}int main(){MGraph G;int source, dest;CreateGraph(G, source, dest);Dijkstra(G, source);cout << G.count[dest] << " " << G.sum[dest] << endl;//remember to free G's memoryfor (int i = 0; i != G.N; ++i) {delete[]G.Edge[i];}delete[]G.Edge;delete[]G.dist;delete[]G.teams;delete[]G.count;delete[]G.pre;delete[]G.sum;}//test file//6 7 0 5//1 2 1 1 2 1//0 1 1//0 2 2//1 2 1//2 3 1//2 4 1//3 5 1//4 5 1
- PAT-A | 1003 | Emergency
- PAT(A)1003 Emergency(最短路+计数)
- PAT A 1003. Emergency (25)
- PAT(A) 1003. Emergency (25)
- PAT-A-1003. Emergency (25)
- PAT A 1003. Emergency (25)
- PAT-A-1003. Emergency (25)
- PAT-A-1003. Emergency (25)
- PAT-A 1003. Emergency (25)
- Pat 1003 Emergency (25)
- ZJU PAT 1003 Emergency
- PAT 1003 Emergency
- PAT 1003 Emergency (25)
- PAT 1003 Emergency
- PAT 1003 Emergency
- PAT 1003 Emergency
- pat 甲级 1003 Emergency
- 【PAT 1003】 Emergency 图论Dijkstra
- EBS 分类账设置
- fastjson生成和解析json数据
- 大数相乘的实现
- 【20150905】NOIP模拟套题01 day2 题解 & 总结
- 基于MongoDB打造.Net的分布式Session子系统
- PAT-A | 1003 | Emergency
- BZOJ 2178 圆的面积并 Simpson积分
- leetcode--Find Minimum in Rotated Sorted Array --C++
- gridview 实现横向分页滑动效果的两种实现方案
- 【第四篇章-android平台MediaCodec】根据编码类型MIME_TYPE获取MediaCodecInfo
- 【面试算法系列】逆序输出一个单链表 - C语言实现
- 彩票摇奖
- Binary Tree Zigzag Level Order Traversal
- 常量指针和指针常量的区别