您的位置:首页 > 其它

PAT_A 1003. Emergency (25)

2016-11-20 15:14 369 查看

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.

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

分析

最开始做pat时,那时的感觉是,哇,太难了!不过经过将近30道题的练习,现在这道题目时,是多么的简单。算法的确很难,但经过学习和练习之后,能够看到自己的进步就很开心!来说这道题,就是一个简单的图,求最短路径,只用dfs就能解决,dijkstra都没用上。

自己做时,对题目的理解有点问题(被1111那道题整恶心了)

所求最大队伍数:最短路径的那几条路中大的队伍数

这里的路是双向的即,p[x][y]=p[y][x]

code

#include<iostream>
#include<vector>
using namespace std;
int path[510][510];
int hands[510];
bool visit[510];
int N,M,C1,C2;
int _min=-1;
int count=0;
int _max=-1;
void dfs(int a,int b,int len,int han)
{
if(a==b)
{
if(_min==-1)
{
_min=len;
count=1;
_max=han;
}
else if(_min>len)
{
count=1;
_min=len;
_max=han;
}else if(_min==len)
{
//题目没说是在最短路中寻找最大人数
//但想想也是啊
count++;
if(han>_max)
_max=han;
}
return ;
}
visit[a]=true;
for(int i=0;i<N;i++)
{
if(path[a][i]!=-1&&visit[i]==false)
dfs(i,b,len+path[a][i],han+hands[i]);
}
visit[a]=false;
}
int main()
{
for(int i=0;i<510;i++)
{
visit[i]=false;
for(int j=0;j<510;j++)
path[i][j]=-1;
}
cin>>N>>M>>C1>>C2;
for(int i=0;i<N;i++)
{
cin>>hands[i];
}

int x,y;
for(int i=0;i<M;i++)
{
cin>>x>>y;
//没说单向路还是双向,那就是双向
cin>>path[x][y];
path[y][x]=path[x][y];
}
dfs(C1,C2,0,hands[C1]);
cout<<count<<" "<<_max<<endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  PAT-A