1003 Emergency

题目链接:
传送门

problem discription:
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 c1 , c2 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 C1
​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-道路的数量,C1和C2-分别表示你目前所在的城市编号以及你必须营救的城市编号。下一行包含N个整数,第i个整数是第i个城市救援队数量。接下来是M行,每行都用3个整数C1、C2和L来描述一条道路,分别表示由道路连接的一对城市和道路的长度。它保证至少存在一条从C1到C2的道路。

输出规格:
对于每一个测试样例,在一行中打印两个数字:C1和C2之间不同最短路径的数量,以及你能召集到的救援队数量的最大值,一行中的所有数字必须用一个空格隔开,行尾不允许有额外的空格。

样例输入:
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

样例示意图:
在这里插入图片描述
题目思路:
这道题目和“城市间的紧急救援”题目类似,是“城市间的紧急救援”题目的弱化版本。
这题有个坑点,是输入的c2可能与c1相等,因此,若用“城市间紧急救援”那题的思路求解,最后输出结果的时候,需进行一下判断,c1是否等于c2,以进行区别输出。

不清楚的同学可以参考一下我往期的csdn分享—“城市间的紧急救援”,这边就不再过多赘述了。
传送门

参考代码:

#include <stdio.h>
#include <string.h>

#define INF 65535

int n,m,c1,c2;
int map[501][501];
int dist[501];//dist[i]记录起始点s到编号为i的顶点的最短路径长度。 
int visited[501];//visited[i]记录编号为i的顶点是否被访问。0-未被访问,1-访问过了。
int Num[501];//Num[i]记录编号为i的顶点所拥有的救援队数量。 
int cnt[501];//cnt[i]记录起始点s到编号为i的顶点的最短路径条数。 
int ccf[501];//ccf[i]记录起始点s到编号为i的城市时所召集的所有救援队数量。 

void init()
{
	int i,j;
	for(i=0;i<n;i++)
	 	Num[i]=0;
	
	for(i=0;i<n;i++)
	{
		for(j=0;j<n;j++)
		 map[i][j]=INF;
	}
} 

void dijkstra(int s)
{
	memset(visited,0,sizeof(visited));
	int i,j;
	
	for(i=0;i<n;i++)
	 dist[i]=map[s][i];
	 
	for(i=1;i<n;i++)
   {
		int min=INF,pos;
		for(j=0;j<n;j++)
		{
			if(visited[j]==0&&dist[j]<min)
			{
			 min=dist[j];
			 pos=j;
			} 
		}
	
	
	visited[pos]=1;
	
	 for(j=0;j<n;j++)
	 {
		if(visited[j]==0&&dist[j]>dist[pos]+map[pos][j])
		{
			dist[j]=dist[pos]+map[pos][j];
			cnt[j]=cnt[pos];
			ccf[j]=ccf[pos]+Num[j];
		}
		else if(visited[j]==0&&dist[j]==dist[pos]+map[pos][j])
		{
			cnt[j]+=cnt[pos];
			if(ccf[j]<ccf[pos]+Num[j])
			  ccf[j]=ccf[pos]+Num[j]; 
		}
	 }
   }
}

int main()
{
	scanf("%d%d%d%d",&n,&m,&c1,&c2);
	
	init();
	
	int i,x,y,z;
	for(i=0;i<n;i++)
	{
		scanf("%d",&Num[i]);
		ccf[i]=Num[i];
		cnt[i]=1;
	 } 
	
	for(i=0;i<m;i++)
	{
		scanf("%d%d%d",&x,&y,&z);
		map[x][y]=map[y][x]=z;
	}
	
	dijkstra(c1);
	
	if(c2==c1)
	 printf("%d %d",cnt[c2],ccf[c2]);
	else 
	printf("%d %d",cnt[c2],ccf[c2]+Num[c1]);
	return 0;
}
  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值