PAT 1003. Emergency (25)

37 篇文章 0 订阅
#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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值