#include <stdio.h>
#include <vector>
using namespace std;
#define MAXCITY 550
struct City{
int id,team,dis;
City(int i,int t=0, int d=0):id(i),team(t),dis(d){}
};
int N,M,C1,C2;
int dis,helper,cnt;//暂存DFS中的距离和帮手数量,以及最短路径数量计数;
int mindis=123123123,maxhelper;//最终的最短距离和最大帮手数量。
int visited[MAXCITY];
int team[MAXCITY];
vector<City> vec[MAXCITY];
void DFS(int from,int to){
if (dis>mindis) {
return;
}
if (from==to) {
if (dis==mindis) {
cnt++;
if (helper>maxhelper) {
maxhelper=helper;
}
}
if (dis<mindis) {
mindis=dis;
maxhelper=helper;
cnt=1;
}
}
for (int i=0; i<vec[from].size(); i++) {
int index=vec[from][i].id;
if (visited[index]==0) {
visited[index]=1;
dis+=vec[from][i].dis;
helper+=vec[from][i].team;
DFS(index, to);
visited[index]=0;
dis-=vec[from][i].dis;
helper-=vec[from][i].team;
}
}
}
int main(){
//freopen("/Users/pantingting/Documents/code/data/input", "r", stdin);
scanf("%d%d%d%d",&N,&M,&C1,&C2);
for (int i=0; i<N; i++) {
scanf("%d",&team[i]);
}
int c1,c2,dis;
for (int i=0; i<M; i++) {
scanf("%d%d%d",&c1,&c2,&dis);
City tmp2(c2,team[c2],dis);
vec[c1].push_back(tmp2);
City tmp1(c1,team[c1],dis);
vec[c2].push_back(tmp1);
}
visited[C1]=1;
helper=team[C1];
DFS(C1,C2);
printf("%d %d\n",cnt,maxhelper);
return 0;
}
PAT 1003. Emergency (25)
最新推荐文章于 2020-04-22 16:28:20 发布