pat 1003. Emergency (25)

https://www.patest.cn/contests/pat-a-practise/1003

#include <cstdio>
#include <vector>
#include <climits>
using namespace std;


pair<int,int> dijstra(vector<vector<int>> g, vector<int> hands,int c1,int c2,int n) {
	vector<bool> v(n, 0);
	vector<int> dist(n, INT_MAX);
	vector<int> chands(n,0);
	vector<int> times(n,0);
	dist[c1] = 0;
	times[c1] = 1;
	chands[c1] = hands[c1];
	for (int i = 0; i < n; i++)
	{
		int tnode = c1, tdist = INT_MAX, thands=0;
		for (int j = 0; j < n; j++)
		{
			if (!v[j] &&
				(dist[j] < tdist ||
					(dist[j] == tdist && chands[j]>thands))) {
				tnode = j;
				tdist = dist[j];
				thands = chands[j];
			}
		}
		//printf("tnode: %d %d %d\n",tnode, tdist,thands);
		v[tnode] = true;
		if (tnode == c2) return make_pair(times[c2],chands[c2]);
		for (int j = 0; j < n; j++)
		{
			if (!v[j]&& g[tnode][j] != INT_MAX) {
				if (dist[j] > dist[tnode] + g[tnode][j]) {
					dist[j] = dist[tnode] + g[tnode][j];
					times[j] = times[tnode];
					chands[j] = chands[tnode] + hands[j];
				}
				else if(dist[j] == dist[tnode] + g[tnode][j]) {
					times[j] = times[j] + times[tnode];
					if (chands[j] < chands[tnode] + hands[j]) {
						chands[j] = chands[tnode] + hands[j];
					}
				}
			}
		}
	}
}

int main()
{
	int n, m, c1, c2, a, b, d;
	scanf("%d %d %d %d",&n,&m,&c1,&c2);
	vector< vector<int> > g(n, vector<int>(n, INT_MAX));
	vector<int> hands(n);
	for (int i = 0; i < n; i++)
	{
		scanf("%d",&hands[i]);
	}
	for (int i = 0; i < m; i++)
	{
		scanf("%d %d %d",&a,&b,&d);
		g[a][b] = g[b][a] = d;
	}

	pair<int,int> times_hands = dijstra(g,hands,c1,c2,n);
	printf("%d %d\n",times_hands.first,times_hands.second);
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值