PAT (Advanced Level) Practise 1003 解题报告

时间:2023-03-08 16:34:43

GitHub

markdownPDF


问题描述

Emergency (25)

时间限制 400 ms

内存限制 65536 kB

代码长度限制 16000 B

判题程序 Standard

作者 CHEN, Yue

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

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, C1 and C2 - 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 C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, 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

大意是:

n个城市m条路,每个城市都有救援小组。已知各边权值,给定起点和重点,求从起点到重点的最短路径条数以及最短路径上的救援小组数目之和。

n<=500.


解题思路

最短路径,可以用dijsktra或深搜(数据不大)。dijsktra多记录一下路径数和小组总数。


代码

dijsktra

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k,n,m,s,t,c1,c2,a[501][501],b[501],c[501],d[501],e[501],f[501]={0};
memset(a,0x3f,sizeof(a));
memset(d,0x3f,sizeof(d));
cin>>n>>m>>c1>>c2;
for (i=0;i<n;i++) cin>>b[i];
for (i=0;i<m;i++)
{
cin>>j>>k>>t;
a[j][k]=a[k][j]=t;
}
c[c1]=b[c1];
d[c1]=0;
e[c1]=1;
for (i=0;i<n;i++)
{
s=0x3f3f3f3f;
t=-1;
for (j=0;j<n;j++)
if ((f[j]==0)&&(d[j]<s))
{
t=j;
s=d[j];
}
if (t==-1) break;
f[t]=1;
for (j=0;j<n;j++)
if (f[j]==0)
if (d[j]>d[t]+a[t][j])
{
d[j]=d[t]+a[t][j];
c[j]=c[t]+b[j];
e[j]=e[t];
}
else
if (d[j]==d[t]+a[t][j])
{
e[j]+=e[t];
if (c[j]<c[t]+b[j]) c[j]=c[t]+b[j];
}
}
cout<<e[c2]<<' '<<c[c2];
return 0;
}

DFS

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
int n,m,s,t,maxt,mind,c1,c2,d,a[501][501],b[501]={0},c[501]={0};
void dfs(int k)
{
int i,j;
if (k==c2)
{
if (d<mind)
{
mind=d;
maxt=t;
s=1;
}
else if (d==mind)
{
s++;
if (t>maxt) maxt=t;
}
}
c[k]=1;
for (i=0;i<n;i++)
if ((c[i]==0)&&(a[k][i]>0))
{
d+=a[k][i];
t+=b[i];
dfs(i);
d-=a[k][i];
t-=b[i];
}
c[k]=0;
}
int main()
{
//freopen("in.txt","r",stdin);
int i,j,k;
memset(a,-1,sizeof(a));
cin>>n>>m>>c1>>c2;
for (i=0;i<n;i++) cin>>b[i];
for (i=0;i<m;i++)
{
cin>>j>>k>>t;
a[j][k]=a[k][j]=t;
}
maxt=0;
mind=0x7FFFFF;
t=b[c1];
d=0;
dfs(c1);
cout<<s<<' '<<maxt;
return 0;
}

提交记录

dijsktra

PAT (Advanced Level) Practise 1003 解题报告

DFS

PAT (Advanced Level) Practise 1003 解题报告