1003. Emergency (25)
来源:互联网 发布:大数据分析师待遇 编辑:程序博客网 时间:2024/05/19 19:12
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
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
IDEA
1.借鉴被人写的。。。自己对图啊,树啊这部分理解的好差啊
2.用深度遍历,计算机最短路径数和经过点的最大权值
3.用到climts.h库中宏定义INT_MAX,表示大数
CODE
#include<iostream>#include<climits>#include<fstream>using namespace std;#define MAX 501int rescue[MAX];int length[MAX][MAX];int visit[MAX];int count,min_len,max_res;int n;void init(){for(int i=0;i<n;i++){visit[i]=0;for(int j=0;j<n;j++){length[i][j]=INT_MAX;}}}void dfs(int from,const int end,int dist,int res){if(from==end){if(dist<min_len){count=1;min_len=dist;max_res=res;}else if(dist==min_len){count++;if(max_res<res){max_res=res;}}return;}if(dist>min_len){return;}for(int i=0;i<n;i++){if(!visit[i]&&length[from][i]!=INT_MAX){visit[i]=1;dfs(i,end,dist+length[from][i],res+rescue[i]);visit[i]=0;}}}int main(){//freopen("input.txt","r",stdin);int m,start,end;count=0;min_len=INT_MAX;cin>>n>>m>>start>>end;for(int i=0;i<n;i++){cin>>rescue[i];}init();while(m--){int c1,c2,dist;cin>>c1>>c2>>dist;if(length[c1][c2]>dist){length[c1][c2]=length[c2][c1]=dist;}}dfs(start,end,0,rescue[start]);cout<<count<<" "<<max_res;//fclose(stdin);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中普通代码块,构造代码块,静态代码块区别及代码示例
- Spring Mvc ajax和json数据格式的配置
- C++和Java写文件(int类型)
- POJ1236【图的强连通(缩点)】
- 【Leetcode】Employees Earning More Than Their Managers
- 1003. Emergency (25)
- [LeetCode] Top K Frequent Elements
- iOS开源项目
- Spinner的使用案例
- python函数定义
- 如何利用jdk来把java文件变成.c
- hdu 1009 FatMouse' Trade(贪心)
- Java多线程面试总结
- 基于FPGA的CAN总线控制器(支持CANFD)