1003. Emergency (25)

考察最短路,以及相同最短路存在时的额外信息最优选择

#include<iostream>
#include<vector>

#define INF 0x6FFFFFFF
typedef struct Node
{
	int nPath;
	int call;
	int dis;
}Node;
std::vector<Node> city;
std::vector<std::vector<int>> map;
std::vector<bool> visit;

std::vector<int> team;

int extract_min()
{
	int num = city.size();
	int min = INF;
	int minIdx = -1;
	for(int i = 0; i < num; ++i)
	{
		if(!visit[i] && city[i].dis < min)
		{
			minIdx = i;
			min = city[i].dis;
		}
	}
	return minIdx;
}
void Dijkstra(int s, int t)
{
	//initial
	int num = map.size();
	city.resize(num);
	for(int i = 0; i < num; ++i)
	{
		city[i].dis = INF;
		city[i].call = 0;
		city[i].nPath = 0;
	}
	city[s].dis = 0; city[s].nPath = 1; city[s].call = team[s];
	visit.assign(num, false);

	while(true)
	{
		//extract min
		int u = extract_min();
		if(u != -1)
		{//extract succeed
			visit[u] = true;
			if(u == t) return;
			//relaxation step
			for(int v = 0; v < num; ++v)
			{
				if(map[u][v] == INF || visit[v]) 
					continue;
				if(city[v].dis > city[u].dis+map[u][v])
				{
					city[v].dis = city[u].dis+map[u][v];
					city[v].call = city[u].call+team[v];
					city[v].nPath = city[u].nPath;
				}
				else if(city[v].dis == city[u].dis+map[u][v])
				{
					city[v].nPath += city[u].nPath;
					if(city[v].call < city[u].call+team[v])
						city[v].call = city[u].call+team[v];
				}
					
			}
		}
		else return;
	}
}
int main()
{
	int n, m, s, t;
	while(scanf("%d%d%d%d",&n,&m,&s,&t)!=EOF)
	{
		map.resize(n);
		for(int i = 0; i < n; ++i)
			map[i].assign(n, INF);
		team.resize(n);
		for(int i = 0; i < n; ++i)
			scanf("%d",&team[i]);
		for(int i = 0; i < m; ++i)
		{
			int a, b, c;
			scanf("%d%d%d",&a,&b,&c);
			if(map[a][b] > c)
			{//erease duplicate edge get the minimum one
				map[a][b] = map[b][a] = c;
			}
		}
		//
		Dijkstra(s, t);
		//printf("%d\n", city[t].dis);
		printf("%d %d\n", city[t].nPath, city[t].call);
	}
	return 0;
}


 

你可能感兴趣的:(pat,ZJU)