PAT1003. Emergency

#include <iostream>
#include <stdio.h>
#include <memory.h>
using namespace std;
#define INT_MAX       100000000   
int numCity,numRoad;
int C1,C2;
int teams[505];
int road[505][505]={0};
bool isReached[505]={false};
long long int minLen = INT_MAX;
long long int curLen = 0;
long long int num = 0;
long long int maxT = -1;
long long int curT = 0;
void dfs(int cur)
{
	if(curLen>minLen)
		return;
	if(cur == C2)
	{
		//reach
		if(curLen<minLen)
		{
			minLen = curLen;
			num=1;
			maxT = curT;
		}else if(curLen == minLen)
		{
			num++;
			if(curT>maxT)
				maxT = curT;
		}
		return;
	}
	for(int i=0;i<numCity;i++)
	{
		if(i!=cur && !isReached[i] && road[cur][i] >0)
		{
			isReached[i] = true;
			curLen+=road[cur][i];
			curT += teams[i];
			dfs(i);
			curT -= teams[i];
			curLen-=road[cur][i];
			isReached[i] = false;
		}
	}
}
int main1003()
{
	scanf("%d %d %d %d",&numCity,&numRoad,&C1,&C2);
	for(int i=0;i<numCity;i++)
	{
		scanf("%d",teams+i);
	}
	
	for(int i=0;i<numRoad;i++)
	{
		int a,b,L;
		scanf("%d %d %d",&a,&b,&L);
		road[a][b] = L;
		road[b][a] = L;
	}
	curT = teams[C1];
	isReached[C1]=true;
	dfs(C1);
	printf("%lld %lld",num,maxT);
	return 0;
}



你可能感兴趣的:(PAT1003. Emergency)