• PAT 甲练习 1003 Emergency


    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:

    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

    dijkstra算法求最短路+路径还原就好了

    Mycode :

    #include<bits/stdc++.h>
    using namespace std;
    typedef long long ll;
    #define inf 0x3f3f3f3f
    typedef pair<int,int> pir;
    struct edge{int to;int cost;};
    vector<edge> E[505];
    int s,des;
    int n,m;
    int d[505];
    int num[505];
    vector<int> pre[505];
    int dijkstra()
    {
        priority_queue<pir,vector<pir>,greater<pir> >q;
        while(!q.empty())q.pop();
        fill(d,d+n,inf);
        d[s]=0;
        pir tmp(0,s);
        q.push(tmp);
        while(!q.empty())
        {
    
            tmp=q.top();q.pop();
            int v=tmp.second;
            if(d[v]<tmp.first)continue;
            for(int i=0;i<E[v].size();i++)
            {
                int j=E[v][i].to;
                if(d[j]>d[v]+E[v][i].cost)
                {
                    pre[j].clear();
                    d[j]=d[v]+E[v][i].cost;
                    pre[j].push_back(v);
                    q.push(pir(d[j],j));
                }
                else if(d[j]==d[v]+E[v][i].cost)
                {
                    pre[j].push_back(v);
                }
            }
        }
        return  d[des];
    }
    int ans1=0;int ans2=0;
    void dfs(int now,int total)
    {
        if(now==s){ans1++;ans2=max(ans2,total+num[now]);return;}
        for(int i=0;i<pre[now].size();i++)
        {
            dfs(pre[now][i],total+num[now]);
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%d%d%d%d",&n,&m,&s,&des);
        for(int i=0;i<n;i++)scanf("%d",&num[i]);
        int p1,p2,cost;
        edge tmp;
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&p1,&p2,&cost);
            tmp.to=p2;tmp.cost=cost;
            E[p1].push_back(tmp);
            tmp.to=p1;tmp.cost=cost;
            E[p2].push_back(tmp);
        }
        dijkstra();
        dfs(des,0);
        printf("%d %d
    ",ans1,ans2);
        return 0;
    }
    
    
  • 相关阅读:
    一个母亲一生撒的八个谎言(含泪推荐)!
    穷人必须做的四件大事
    八种人不太可能驰骋职场,有你吗?
    是谁让我如此忧伤?
    拒绝平淡:我用十年的时间名满天下
    高性能利器!华为云MRS ClickHouse重磅推出!
    MySQL 连接为什么挂死了?
    解读登录双因子认证(MFA)特性背后的TOTP原理
    云图说|初识数据仓库服务:云时代的数据分析助手
    号外!5G+X联创营华为云官网上线,5G 创业春天来了!
  • 原文地址:https://www.cnblogs.com/linruier/p/10035287.html
Copyright © 2020-2023  润新知