PAT 甲级 1003  Emergency

编程入门 行业动态 更新时间:2024-10-26 08:22:00

1003 Emergency (25 point(s))

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

Experiential Summing-up

This question is a classical dijkstra algorithm involve point weight , edge weight and quantity of short distance.  Nothing else. 

(The purpose of using English to portray my solution is that to exercise the ability of my expression of English and accommodate PAT advanced level's style.We can make progress together by reading and comprehending it. Please forgive my basic grammar's and word's error. Of course, I would appreciated it if you can point out my grammar's and word's error in comment section.( •̀∀•́ ) Furthermore, Big Lao please don't laugh at me because I just a English beginner settle for CET6    _(:з」∠)_  )

Accepted Code

#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int maxn=510;
const int INF=0x3fffffff;
int g[maxn][maxn],d[maxn],C[maxn],c[maxn],num[maxn],n;
bool flag[maxn];
void Dijkstra(int s)
{
	fill(d,d+maxn,INF);
	memset(c,0,sizeof(c));
	memset(flag,0,sizeof(flag));
	memset(num,0,sizeof(num));
	d[s]=0;
	c[s]=C[s];
	num[s]=1;
	for(int i=0;i<n;++i)
	{
		int u=-1,min=INF;
		for(int j=0;j<n;++j)
		{
			if(d[j]<min&&flag[j]==false)
			{
				min=d[j];
				u=j;
			}
		}
		if(u==-1)
			return ;
		flag[u]=true;
		for(int v=0;v<n;++v)
		{
			if(g[u][v]!=INF&&flag[v]==false)
			{
				if(d[u]+g[u][v]<d[v])
				{
					d[v]=d[u]+g[u][v];
					c[v]=c[u]+C[v];
					num[v]=num[u];
				}
				else if(d[u]+g[u][v]==d[v])
				{
					if(c[u]+C[v]>c[v])
					{
						c[v]=c[u]+C[v];
					}
					num[v]+=num[u];
				}
			}
		}
	}
}
int main()
{
    int m,c1,c2,a,b,v;
	scanf("%d %d %d %d",&n,&m,&c1,&c2);
	for(int i=0;i<n;++i)
		scanf("%d",&C[i]);
	fill(g[0],g[0]+maxn*maxn,INF);
    for(int i=0;i<m;++i)
	{
		scanf("%d %d %d",&a,&b,&v);
		g[a][b]=g[b][a]=v;
	}
	Dijkstra(c1);
	printf("%d %d\n",num[c2],c[c2]);
    return 0;
}

 

更多推荐

PAT 甲级 1003  Emergency

本文发布于:2023-06-13 07:58:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1361282.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:PAT   Emergency

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!