L2-001 emergency (25 points)

`As the person in charge of emergency rescue team of a city, you have a special map of the country. Show on the road map has more rapid dispersion of the city and some of the connections to the city. And a number of rescue teams per each city connecting the two cities fast road length is marked on the map. While other cities have emergency phone call to you, your mission is to lead your rescue team rushed to the incident as soon as possible, at the same time along the way to convene as many rescue teams.

Input format:
input of the first line gives four positive integers N, M, S, D, where N (2≤N≤500) is the number of the city, the city assumed way numbered 0 ~ (N-1); M is the number of fast roads; S is the point of departure cities numbered; D is the number of urban destinations.

The second line gives a positive integer N, which is the number of i-th i rescue team of cities, separated by spaces between numbers. Subsequent M rows, each row gives a fast way of information, namely: 1 urban, urban length of 2, fast road, separated by a space intermediate, figures are an integer of not more than 500. Input to ensure a viable and optimal solution unique rescue.

Output format:
The first line number of the shortest path and output largest number of rescue teams can muster. The second line is output from D to S path through the city number. Between numbers separated by spaces, the end of the output can not have extra space.

Sample input:
. 4. 3. 5 0
20 is 30 10 40
0. 1. 1
. 1. 3 2
0. 3. 3
0 2 2
2 2. 3

Output Sample:
2 60
0. 1. 3

/ * For the first time with a priority queue for a long time do not do it, then went to the dijstra algorithm looked good again, and then combine with others to see their own writing, eventually made it out (he has written, or too little, advice freshman mentees have to learn ah), refuel ah * /

在这里插入代码片#include<iostream>
#include<queue>
#include<string.h>
using namespace std;

const int inf = 1e6;      
int city[505][505];     //用来标记一个城市到另一个城市的距离	 
int pre[505];           //最短路径中城市的前驱 
bool sign[505];         //标记一个城市是否在最短路径中 
int arm[505];           //该城市的军队数目 
int sum[505];           //经过最短路径中当前城市的军队总数 
int dis[505];           //该城市到起点的最短距离 
int art[505] = {0};     //最短路径的条数 
struct node{
	int n;         //城市 
	int dis;       //该城市距离起点的最短距离 
	node(int _n,int _dis)
	{
		n = _n;
		dis = _dis;
	}
	bool operator < (const node &a)const   //优先队列的优先性,距离起点近的优先级别高 
	{
		return a.dis < dis;
	}
};
int n,m,be,en;

void dijstra(int s)
{
	memset(dis,inf,sizeof(dis));
	dis[s] = 0;                        //起点到起点的距离为0	 
	sum[s] = arm[s];				   //起点到起点军队总数 
	art[s] = 1;							//起点到起点的最短路径条数为  1 
	priority_queue<node>q;
	q.push(node(s,dis[s]));
	while(!q.empty())
	{
		node u = q.top();
		q.pop();
		if(sign[u.n])
		continue;
		sign[u.n] = true;
		for(int i=0;i<505;i++)
		{
			if(city[u.n][i]!=inf)
			{
				if(sign[i])
				continue;
				if(dis[i] > dis[u.n] + city[u.n][i])		//距离相比较短 
				{
					dis[i] = dis[u.n] + city[u.n][i];
					sum[i] = arm[i] + sum[u.n];
					pre[i] = u.n;
					q.push(node(i,dis[i]));
					art[i] = art[u.n];                     //更新最短路径条数 
				}
				else if(dis[i] == dis[u.n]+city[u.n][i])	//距离相等,但是军队总数较多 
				{
					if(sum[i] < sum[u.n] + arm[i])
					{
						sum[i] = arm[i] + sum[u.n];
						pre[i] = u.n;
						q.push(node(i,dis[i]));
					}
					art[i]+=art[u.n];                        //更新最短路径条数 
				}
			}
		}
	}
} 
void print(int be,int en)                                  //遍历起点到终点经过的城市 
{
	if(be == en)
	{
		printf("%d",en);
		return ;
	}
	else
	{
		print(be,pre[en]);
		printf(" %d",en);
	}
}

int main()
{
	scanf("%d %d %d %d",&n,&m,&be,&en);
	for(int i=0;i<n;i++)
	{
		int a;
		scanf("%d",&a);
		arm[i] = a;
	}
	memset(pre,-1,sizeof(pre));
	memset(sum,0,sizeof(sum));
	memset(sign,false,sizeof(sign));
	for(int i=0;i<505;i++)
	for(int j=0;j<505;j++)
	city[i][j] = inf;
	for(int i=0;i<m;i++)
	{
		int a,b,w;
		scanf("%d %d %d",&a,&b,&w);
		city[a][b] = w;
		city[b][a] = w;
	}
	dijstra(be);
	cout<<art[en]<<" "<<sum[en]<<endl;
	print(be,en);
}
发布了1 篇原创文章 · 获赞 0 · 访问量 9

Guess you like

Origin blog.csdn.net/chenchanghie/article/details/104211048