PAT.A1003 Emergency

Back to ContentsInsert picture description here

Title

Given N cities, M undirected edges. There are a certain number
of rescue teams in each city , and the border rights of all sides are known. Now give the starting point and the end point, and find the sum of the number of shortest paths from the starting point to the end point and the number of rescue teams on the shortest path. If there are multiple shortest paths, the sum of the number of outputs is the largest.

Sample (can be copied)

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
//output
2 4

important point

  1. You cannot use memset when initializing dis
  2. This question does not have a test point where the starting point and the ending point are not connected
  3. Dijkstra's idea is: always find the shortest distance point that the current connected block can go, and add this point to the connected block
#include <bits/stdc++.h>
using namespace std;

int n,m,st,ed;//n个城市,m条边,起点,终点 
int city[510],dis[510],num[510],team[510];//存放每个城市的救援队数量,最短距离 ,标记访问过的城市 
bool vis[510]={false};
int G[510][510];//图 
void Dijkstra(){
	fill(dis,dis+n,INT_MAX);
	dis[st]=0;
	num[st]=1;
	team[st]=city[st];
	while(!vis[ed]){
		int MIN=INT_MAX,v;//找出目前距离最短的还没有被访问的城市
        for(int i=0;i<n;++i)
            if(!vis[i]&&MIN>dis[i]){
                MIN=dis[i];
                v=i;
            }
        vis[v]=true;//标记
        for(int i=0;i<n;++i)
            if(!vis[i]&&G[v][i]!=0&&dis[v]+G[v][i]<dis[i]){
                dis[i]=dis[v]+G[v][i];//更新最短路径长度
                num[i]=num[v];//更新最短路径数量
                team[i]=team[v]+city[i];//更新城市的救护队数量
            }else if(G[v][i]!=0&&dis[v]+G[v][i]==dis[i]){
                num[i]+=num[v];//增加最短路径数量
                team[i]=max(team[i],team[v]+city[i]);//找出能够召集最多的城市救护队数量
            }
	}
}
int main(){
	cin>>n>>m>>st>>ed;
	for(int i=0;i<n;i++)scanf("%d",&city[i]);
	int a,b,c;
	while(m--){
		scanf("%d%d%d",&a,&b,&c);
		G[b][a]=G[a][b]=c;
	}
	Dijkstra();
	cout<<num[ed]<<" "<<team[ed]<<endl;
	return 0;
} 
Published 177 original articles · praised 5 · visits 6653

Guess you like

Origin blog.csdn.net/a1920993165/article/details/105565406