PTA 1003 Emergency
问题描述
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 Specification:
Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤) - 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 Specification:
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.
Sample Input:
5 6 0 2 1 2 1 5 3 0 1 1 0 2 2 0 3 1 1 2 1 2 4 1 3 4 1Sample Output:
2 4
思路:Floyd+DFS
遇到的问题:
1.一开始题看错了,问路径数量想当然地以为是问最短路径长度,又撞上了如此巧的Sample,惨
2.Floyd翻了个车,里面的if语句条件写的有点问题,碰巧又错得很艺术,简单的样例都能过,大囧
总结:太久不摸键盘了,手生了,以后还是得多多练习
代码:
#include <iostream> using namespace std; int n,m,c1,c2,dist[500][500],res[500],map[500][500],len=0,sup,ressum,resmax=0,count=0; // n城市数,m路径数,c1、c2起止城市,dist存距离,res存救援队数量,map存路径长度 // len存DFS路径长度,sup存c1到c2距离,ressum存DFS过程中救援队数量,resmax存最大救援队数量,count存路径数 bool proc[500]; //存储DFS过程中城市经过情况,防止重复经过某一城市 void dfs(int x){ if (len>sup) return; // 走过距离超过c1、c2距离直接返回 if (x==c2){ //到达c2 if (len==sup){ //恰好是最短的路径 count++; //路径数+1 if (ressum>resmax) resmax=ressum; //判断此时救援队数量 } return; } for (int i=0;i<n;i++){ if (map[x][i]>=0 && proc[i]==false){ // i是x可以通往的城市 len+=map[x][i]; ressum+=res[i]; if (len<=sup){ proc[i]=true; //上述处理DFS准备 dfs(i); proc[i]=false; //下述处理DFS结束后还原 } len-=map[x][i]; ressum-=res[i]; } } return; } int main(){ int x,y,dis; //初始化 for (int i=0;i<=499;i++){ proc[i]=false; for (int j=0;j<=499;j++){ dist[i][j]=map[i][j]=-1; } } //读入 cin >> n >> m >> c1 >> c2; for (int i=0;i<n;i++) cin >> res[i]; for (int i=0;i<m;i++){ cin >> x >> y >> dis; dist[y][x]=dist[x][y]=map[y][x]=map[x][y]=dis; } //特殊情况判断 if (c1==c2){ cout << "1 " << res[c1]; return 0; } //Floyd处理 for (int k=0;k<n;k++) for (int i=0;i<n;i++) for (int j=0;j<n;j++) if ((dist[i][j]<0 && dist[i][k]>=0 && dist[k][j]>=0)|| (dist[i][j]>dist[i][k]+dist[k][j]&&dist[i][k]>=0&&dist[k][j]>=0)) dist[i][j]=dist[i][k]+dist[k][j]; //DFS sup=dist[c1][c2]; proc[c1]=true; ressum=res[c1]; dfs(c1); //输出 cout << count << " " << resmax; return 0; }

浙公网安备 33010602011771号