1003. Emergency (25)
来源:互联网 发布:阿里云汽车荣威rx5 编辑:程序博客网 时间:2024/06/07 19:01
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
这道题目是典型的dfs问题,通过遍历临界矩阵,找到问题的解。
代码:
#include<iostream>#include<cstdlib>#include<vector>#include<cstring>using namespace std;int n,m,c1,c2;int **g;int *flag;int *team;vector<int> path;//存放路径(此问题虽然用不到,但是有些深搜问题需要输出最短路径)int min_length=10000000;//存放最短路径int max_team;//存放最多队伍int pl;//临时存放路径长度int mt;//临时存放最大队伍数量int shortnum=1;//存放最短路径的条数void dfs(int s);int main(){cin>>n>>m>>c1>>c2;g=(int **)malloc(n*sizeof(int *));flag=(int *)malloc(n*sizeof(int));memset(flag,0,4*n);for(int i=0;i<n;i++){g[i]=(int *)malloc(n*sizeof(int));memset(g[i],0,4*n);}team=(int *)malloc(n*sizeof(int));for(int i=0;i<n;i++){cin>>team[i];}for(int i=0;i<m;i++){int a,b,c;cin>>a>>b>>c;g[a][b]=c;g[b][a]=c;}flag[c1]=1;path.push_back(c1);mt+=team[c1];dfs(c1);cout<<shortnum<<" "<<max_team;return 0;}void dfs(int s){if(s==c2){//当搜索到目的地时int f=0;if(pl<min_length){shortnum=1;max_team=0;min_length=pl;f=1;}else if(pl==min_length){shortnum++;f=1;}if(mt>max_team&&f==1) max_team=mt;return;}for(int i=0;i<n;i++){ if(g[s][i]==0) continue; if(flag[i]!=0) continue; flag[i]=1; pl+=g[s][i]; mt+=team[i]; path.push_back(i); dfs(i); path.pop_back(); flag[i]=0; mt-=team[i]; pl-=g[s][i];}}
- 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)
- Tarjan求图的割点、桥与双连通分量
- [SCOI2010]连续攻击游戏 匈牙利算法
- Hibernate注解
- SQL 语句(一)--------------模式及表的定义
- 2017.7.11
- 1003. Emergency (25)
- 如何在C中调用C++函数 与 如何在C++中调用C的函数(待修改)
- HDU
- bzoj1925: [Sdoi2010]地精部落
- sdoi 2009 HH去散步 矩阵乘
- IT运维管理必备工具大全,让你成为真正的高手!
- 双缓冲绘图
- JZOJ100045. 【NOIP2017提高A组模拟7.13】好数
- Sublime Text2.0.2注册码-亲测可用