您的位置:首页 > 其它

PAT 1003. Emergency (25)---深搜

2016-05-08 22:42 381 查看

1003. Emergency (25)

时间限制
400 ms

内存限制
65536 kB

代码长度限制
16000 B

判题程序
Standard

作者
CHEN, Yue

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.

#include <iostream>
using namespace std;
const int INFINITY=1000;

int n,m,loc,dtm;
int* mark;
int* teams;
int** map;
int mindis;
int maxtem;
int num;

void dfs(int str,int end,int dis,int teamsum){
if(dis>INFINITY)return;
if(str==end){
if(dis<mindis){
num=1;
mindis=dis;
maxtem=teamsum;
}else if(dis==mindis){
num++;
if(teamsum>maxtem)
maxtem=teamsum;
}
return;
}

for(int i=0;i<n;i++){
if(mark[i]==0&&map[str][i]<INFINITY){
mark[i]=1;
dfs(i,end,dis+map[str][i],teamsum+teams[i]);
mark[i]=0;
}
}
}

int main(){
cin>>n>>m>>loc>>dtm;
teams=new int
;
mark=new int
;
map=new int*
;
for(int i=0;i<n;i++)
{
cin>>teams[i];
mark[i]=0;
map[i]=new int
;
for(int j=0;j<n;j++){
map[i][j]=INFINITY;
}
}
for(int j=0;j<m;j++){
int a,b,x;
cin>>a>>b>>x;
map[a][b]=map[b][a]=x;
}
mindis=INFINITY;
dfs(loc,dtm,0,teams[loc]);
cout<<num<<" "<<maxtem;
//system("pause");
return 0;
}
最后一个测试点超时。未解决。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: