【一只蒟蒻的刷题历程】 【PAT (Advanced Level) Practice】 1003 紧急情况

As an emergency rescue team leader of a city, you are given a special map of your country. The map shows several scattered cities connected by some roads. Amount of rescue teams in each city and the length of each road between any pair of cities are marked on the map. When there is an emergency call to you from some other city, your job is to lead your men to the place as quickly as possible, and at the mean time, call up as many hands on the way as possible.

Input Specification:

Each input file contains one test case. For each test case, the first line contains 4 positive integers: N (≤500) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C​1​​ and C​2​​ - the cities that you are currently in and that you must save, respectively. The next line contains N integers, where the i-th integer is the number of rescue teams in the i-th city. Then M lines follow, each describes a road with three integers c​1​​, c​2​​ and L, which are the pair of cities connected by a road and the length of that road, respectively. It is guaranteed that there exists at least one path from C​1​​ to C​2​​.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C​1​​ and C​2​​, and the maximum amount of rescue teams you can possibly gather. All the numbers in a line must be separated by exactly one space, and there is no extra space allowed at the end of a line.

Sample Input:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

Sample Output:

2 4


题目大意:

作为城市的紧急救援团队负责人,您将获得一张您所在国家的特殊地图。该地图显示了一些通过道路连接的分散城市。地图上标出了每个城市的救援队数量以及每对城市之间的每条道路的长度。当从其他城市接到您的紧急电话时,您的工作是尽快带领您的士兵前往该地点,同时,在途中尽可能多地动手。

输入规格:

每个输入文件包含一个测试用例。对于每个测试用例,第一行包含4个正整数:N(≤500)-城市数量(并且城市从0到N-1编号),M-道路数量,C 1和C 2-您当前所在的城市和必须保存的城市。下一行包含N个整数,其中第i个整数是第i个城市中救援队的数量。然后是M条线,每条线描述了一条具有三个整数c1,c2和L的道路,分别是一条道路和该条道路的长度所连接的一对城市。保证从C 1到C 2至少存在一条路径。

输出规格:

对于每个测试用例,在一行中打印两个数字:C 1和C 2之间最短路径的不同数量以及可以收集的最大救援团队数量。一行中的所有数字必须完全由一个空格分隔,并且在行尾不允许有多余的空格。

样本输入:

5 6 0 2
1 2 1 5 3
0 1 1
0 2 2
0 3 1
1 2 1
2 4 1
3 4 1

样本输出:

2 4


思路:

dijkstra + 优先队列优化

此题核心:

if(dis[v] > dis[u]+len) //可以通过中间点u优化路径
{ 
	dis[v] = dis[u]+len;  //更新距离
	num[v] = num[u];      //继承路径数
	numrescue[v] = numrescue[u]+rescue[v];//更新救援队数量
}
else if(dis[v] == dis[u] +len) //如果路径距离相等
{
    num[v] += num[u];    //加上到达上一点的路径数
    if(numrescue[v] < numrescue[u]+rescue[v])
    //看走哪条路,得到的救援队数量更多,要尽可能多
    numrescue[v] = numrescue[u]+rescue[v];
}  

代码:

#include <iostream>
#include <algorithm>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdio>
#include <cmath>
#include <queue>
#include <set>
#include <map>
using namespace std;
struct node{
	int v;
	int len;
	node(int a,int b){v=a;len=b;}
	friend bool operator<(node a,node b){  //按len从小打到排列 
		return a.len>b.len;
	}
};

const int maxn=550;
const int inf = 1<<27;  //无法到达 
vector<node> g[maxn];  //邻接表存图 
priority_queue<node> q; //优先队列优化 
int rescue[maxn]; //各地救援队数量 
int numrescue[maxn],num[maxn]={0},dis[maxn]; 
//最优救援队数量 , 最短路径条数 , 最短距离 
int n,m,c1,c2;
bool vis[maxn]={false};//标记是否入过队

void dijk(int s)
{
	fill(dis,dis+maxn,inf);
	dis[s]=0;   //初始化距离
	num[s]=1;   //初始化路径
	numrescue[s]=rescue[s];  //起点的最优救援队数量为该点的救援队数量
	q.push(node(s,dis[s]));  //入队列
	while(!q.empty())
	{
		int u=q.top().v;
		q.pop();
		if(vis[u]) continue;
		vis[u]=1;
		for(int i=0;i<g[u].size();i++)
		{
			int v=g[u][i].v;
			int len=g[u][i].len;
			if(vis[v]==false)
			{
				if(dis[v] > dis[u]+len) //可以通过中间点u优化路径
				{ 
					dis[v] = dis[u]+len;  //更新距离
					num[v] = num[u];      //继承路径数
					numrescue[v] = numrescue[u]+rescue[v];//更新救援队数量
				}
				else if(dis[v] == dis[u] +len) //如果路径距离相等
				{
				    num[v] += num[u];    //加上到达上一点的路径数
				    if(numrescue[v] < numrescue[u]+rescue[v])
				    //看走哪条路,得到的救援队数量更多,要尽可能多
				    numrescue[v] = numrescue[u]+rescue[v];
				}  
				
				q.push(node(v,dis[v])); //入队
			}
			
		}
	}
}
int main() 
{
   
   cin>>n>>m>>c1>>c2;
   for(int i=0;i<n;i++)
     cin>>rescue[i];   
     
   int a,b,len;
   for(int i=0;i<m;i++)
   {
   	   cin>>a>>b>>len;
   	   g[a].push_back(node(b,len)); //无向图
   	   g[b].push_back(node(a,len));
   }
   
   dijk(c1);  
   cout<<num[c2]<<" "<<numrescue[c2]; //输出结果
    return 0;
}

原创文章 32 获赞 35 访问量 1520

猜你喜欢

转载自blog.csdn.net/weixin_45260385/article/details/105889513
今日推荐