您的位置:首页 > 其它

PAT 1003. Emergency (25)

2014-10-29 10:43 330 查看
#include<iostream>
#include<vector>
using namespace std;
int n, m, C1, C2;
vector<int> team, path;
int road[501][501] = { 0 };//如果为0是该两点间没有路
int len = 0;
int vst[501] = { 0 };
int helpers = 0, minpath = -1, cnt = 0, maxhelpers = 0;
void dfs(int v){
vst[v] = 1;
if (v == C2)
{
//		path.push_back(v);
len = 0;
helpers = team[C1];
for (int i = 0; i < path.size() - 1;){
int c1 = path[i], c2 = path[++i];
len += road[c1][c2];
helpers += team[c2];
}
if (minpath == -1)	{
minpath = len; maxhelpers = helpers;
cnt = 1;
}
else if (len < minpath){
minpath = len; cnt = 1; maxhelpers = helpers;
}
else if (len == minpath){
cnt++;
if (maxhelpers < helpers)
maxhelpers = helpers;
}
return;
}
else{
for (int i = 0; i < n; i++){
if (!vst[i] && road[v][i] != 0){
path.push_back(i);
dfs(i);
path.pop_back();
vst[i] = 0;
}
}
}
return;
}
int main(){
cin >> n >> m >> C1 >> C2;
for (int i = 0; i < n; i++){
int person; cin >> person;
team.push_back(person);
}
for (int i = 0; i < m; i++){
int c1, c2, l; cin >> c1 >> c2 >> l;
road[c1][c2] = road[c2][c1] = l;
}
path.push_back(C1);
dfs(C1);
cout << cnt << " " << maxhelpers;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: