PAT (Advanced Level) Practice 1003 Emergency (25 分)

2020-09-28 11:04:24 浏览数 (1)

1003 Emergency (25 分)

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:

代码语言:javascript复制
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:

代码语言:javascript复制
2 4

题意:n个点m条边的无向图,求给定起始点到终点的最小路径花费最小路径数目,并输出最大所经点权值和

思路:500个点的dijkstra应该可过,设置三个数组,dis[i]表示起始点到第i个点的最小值,cnt[i]表示起始点到达第i个点的最短路条数,person[i]就是起始点到达第i个点最大所经点权和,val[i][j]表示i到j的边权,num[i]表示第i个点点权~

在dijktstra运行过程中每次循环是找出取出的一个未被标记并且到起始点距离最小的点u,然后以u为起始点找u的出边所连的点去更新dis数组,在这中间

如果dis[u] val[u][v]<dis[v],那么就更新dis[v],且cnt[v]=cnt[u],person[v]=person[v] person[u];

如果dis[u] val[u][v]=dis[v],那么cnt[v]=cnt[v] cnt[u],person[v]=max(person[v],person[u] num[i]);

最后输出cnt[终点]和person[终点]就好~

代码语言:javascript复制
// luogu-judger-enable-o2
#include<bits/stdc  .h>
#include<unordered_set>
#define rg register ll
#define inf 2147483647
#define min(a,b) (a<b?a:b)
#define max(a,b) (a>b?a:b)
#define ll long long
#define maxn 505
#define lb(x) (x&(-x))
const double eps = 1e-6;
using namespace std;
inline ll read()
{
	char ch = getchar(); ll s = 0, w = 1;
	while (ch < 48 || ch>57) { if (ch == '-')w = -1; ch = getchar(); }
	while (ch >= 48 && ch <= 57) { s = (s << 1)   (s << 3)   (ch ^ 48); ch = getchar(); }
	return s * w;
}
inline void write(ll x)
{
	if (x < 0)putchar('-'), x = -x;
	if (x > 9)write(x / 10);
	putchar(x % 10   48);
}
ll n,m,c1,c2;
ll val[maxn][maxn],num[maxn],dis[maxn],cnt[maxn],person[maxn],vis[maxn];
//dis[i]表示到达i点的最小距离,cnt[i]表示到达i点最短路的条数,person[i]表示到达i点能召集最大人手数
inline void dijkstra()
{
   fill(dis,dis maxn,inf);
   dis[c1]=0,cnt[c1]=1,person[c1]=num[c1];
   for(rg i=0;i<n;i  )
   {
       ll u=-1,minn=inf;
       for(rg j=0;j<n;j  )
       {
           if(dis[j]<minn&&!vis[j])
           {
               minn=dis[j],u=j;
           }
       }
       if(u==-1)break;
       vis[u]=1;
       //cout<<u<<endl;
       for(rg v=0;v<n;v  )
       {
           if(val[u][v]!=inf&&!vis[v])
           {
               if(val[u][v] dis[u]<dis[v])
               {
                   dis[v]=val[u][v] dis[u];
                   person[v]=person[u] num[v];
                   cnt[v]=cnt[u];
               }
                else if(val[u][v] dis[u]==dis[v])
               {
                   person[v]=max(person[v],person[u] num[v]);
                   cnt[v] =cnt[u];
               }
           }
       }
   }
}
int main()
{
    cin>>n>>m>>c1>>c2;
    fill(val[0],val[0] maxn*maxn,inf);
    for(rg i=0;i<n;i  )num[i]=read();
    for(rg i=0;i<m;i  )
    {
        ll a,b,c;
        a=read(),b=read(),c=read();
        val[a][b]=val[b][a]=c;
    }
    dijkstra();
    cout<<cnt[c2]<<" "<<person[c2]<<endl;
    return 0;
}

0 人点赞