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;
    }
    
    
  • 相关阅读:
    Win10 安装 Oracle32bit客户端 提示:引用数据不可用于验证此操作系统分发的先决条件
    ORACLE 拆分逗号分隔字符串函数
    PLSQL 中文乱码
    不要把分层当做解耦!
    MySQL 迁移到 PG 怎么做
    在 MySQL 创造类似 PipelineDB 的流视图(continuous view)
    TeamViewer 的替代品 ZeroTier + NoMachine
    所有 HTML attribute
    使用PG的部分索引
    基于 500 份标注数据用深度学习破解验证码
  • 原文地址:https://www.cnblogs.com/linruier/p/10035287.html
Copyright © 2011-2022 走看看