1003. Emergency (25)
来源:互联网 发布:数据库有5个事务 编辑:程序博客网 时间:2024/05/17 06:29
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.
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 1
Sample Output
2 4
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.
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 1
Sample Output
2 4
#include<cstdio>#include<algorithm>#include<string.h>#include<queue>#include<map>using namespace std;int city[505][505];int teamnum[505];int dijkstra[505];bool isVisited[505];int shortest,sumroad,sumteam;const int INF = 10000000;void init(){ for(int i = 0; i < 505;i ++) isVisited[i] = false;}void Dijkstra(int n,int begin,int end){ for(int i = 0; i < n;i ++) dijkstra[i] = city[begin][i]; isVisited[begin] = true; for(int i = 0; i < n - 1;i ++){ int min = INF; int flag = INF; for(int j = 0;j < n;j ++) if(isVisited[j] == false && dijkstra[j] < min){ min = dijkstra[j]; flag = j; } isVisited[flag] = true; if(flag == end) break; for(int k = 0; k < n; k ++) if(isVisited[k] == false && dijkstra[flag] + city[flag][k] < dijkstra[k]) dijkstra[k] = dijkstra[flag] + city[flag][k]; }}void DFS(int begin,int end,int n,int curDis,int curTeam){ isVisited[begin] = true; if(begin == end){ if(curDis == shortest){ sumroad ++; if(curTeam > sumteam) sumteam = curTeam; } return; } for(int i = 0;i < n; i ++){ if(isVisited[i] == false && city[begin][i] != INF) if(curDis + city[begin][i] > shortest) continue; else{ DFS(i,end,n,curDis + city[begin][i],curTeam + teamnum[i]); isVisited[i] = false; //关键 } } return;}int main(void){ int N,M,C1,C2; scanf("%d %d %d %d",&N,&M,&C1,&C2); for(int i = 0;i < N;i ++) scanf("%d",&teamnum[i]); for(int p = 0;p < N;p ++) for(int q = 0;q < N;q++) if(p == q) city[p][q] = 0; else city[p][q] = INF; for(int i = 0;i < N;i ++) dijkstra[i] = INF; for(int i = 0;i < N;i ++) isVisited[i] = false; while(M){ int a,b,len; scanf("%d %d %d",&a,&b,&len); city[a][b] = len; city[b][a] = len; M--; } Dijkstra(N,C1,C2); shortest = dijkstra[C2]; init(); DFS(C1,C2,N,0,teamnum[C1]); printf("%d %d",sumroad,sumteam); return 0;}
0 0
- 1003. Emergency (25)
- 1003. Emergency (25)-PAT
- (PAT)1003. Emergency (25)
- 1003. Emergency (25)
- 【C++】1003. Emergency (25)*
- 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- PAT 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- [PAT]1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- 1003. Emergency (25)
- Java线程池使用说明
- Java的几种访问权限
- poj 2392 多重背包
- 1002. A+B for Polynomials (25)
- ASP.NET一款免费富文本(RichText)编辑器KindEditor,实现在线排版编辑文章。这里简单介绍下如何使用
- 1003. Emergency (25)
- 1004. Counting Leaves (30)
- 杭电---2053 Switch Game
- LeetCode[172]Factorial Trailing Zeroes
- poj1466
- 1005. Spell It Right (20)
- 学分平均绩计算方法
- Struts2学习笔记一
- hasOwnProperty()与for in的区别