首页 > 其他 > 详细

PAT Advanced 1003

时间:2019-09-01 22:58:53      阅读:73      评论:0      收藏:0      [点我收藏+]

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 (≤) - the number of cities (and the cities are numbered from 0 to N1), 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 Nintegers, 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

寻找从c1到c2的最短路径,并使该路径上遇到的救援队人数最大。用dijkstra算法求最短路长度+记录从起点到每个城市遇到的救援队人数即可解决。当找到两条同距离的路径时,选择人数最多的一条。

#include <bits/stdc++.h>
using namespace std;
int N,M,C1,C2;
int teams[510];
struct Edge
{
    int to;
    int d;
    bool operator < (const Edge &a)const{
        return d>a.d;
    }
};

bool visit[510];

int main()
{
    memset(teams,0,sizeof teams);
    int cc1,cc2,L;
    cin>>N>>M>>C1>>C2;
    vector<vector<Edge> > G(N,vector<Edge>());
    for(int i=0;i<N;i++)
        cin>>teams[i];

    for(int i=0;i<M;i++)
    {
        cin>>cc1>>cc2>>L;
        G[cc1].push_back((Edge){cc2,L});
        G[cc2].push_back((Edge){cc1,L});
    }
    memset(visit,false,sizeof visit);
    vector<int> d(N,INT_MAX);
    d[C1]=0;
    priority_queue<Edge> Q;
    Q.push((Edge){C1,0});
    vector<int> T(N,INT_MAX);
    vector<int> p(N,0);
    p[C1]=1;
    T[C1]=teams[C1];
    while(!Q.empty())
    {
        Edge tmp=Q.top();Q.pop();
        int u=tmp.to;
        if(visit[u])continue;
        visit[u]=true;
        for(int i=0;i<G[u].size();i++)
        {

            Edge& e=G[u][i];
            int v=e.to;
            if(d[v]>d[u]+e.d)
            {
                d[v]=d[u]+e.d;

                p[v]=p[u];
                T[v]=T[u]+teams[v];
                Q.push((Edge){v,d[v]});
            }else if(d[v]==d[u]+e.d)
            {

                p[v]+=p[u];
                if(T[u]+teams[v]>T[v])
                T[v]=T[u]+teams[v];

            }
        }
    }
    cout<<p[C2]<< <<T[C2]<<endl;
    return 0;
}

 

PAT Advanced 1003

原文:https://www.cnblogs.com/zest3k/p/11443949.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!