zoukankan      html  css  js  c++  java
  • 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;
    }
    
    
  • 相关阅读:
    如果世界变慢为原先的百万分之一
    N880e 刷机记录和一些经验
    Linux Shell Commands 常用Linux命令行
    适用于单选的jQuery Autocomplete插件SelectToAutocomplete
    PHP5.3 中的Warning: date_default_timezone_set
    Apache中RewriteCond规则和QUERY_STRING参数
    网站网络编辑日常工作流程
    网站编辑与传统媒体编辑的区别及特点
    使用JCaptcha生成验证码
    Debian 7 Wheezy 安装Oracle JDK6, JDK5和Tomcat6
  • 原文地址:https://www.cnblogs.com/linruier/p/10035287.html
Copyright © 2011-2022 走看看