PAT A1003 Emergency

版权声明:Dirichlet_zju https://blog.csdn.net/Dirichlet_zju/article/details/84585314
提交时间 状态 分数 题目 编译器 耗时 用户
2018/11/28 16:39:05

答案正确

25 1003 C++ (g++) 5 ms Dirichlet
测试点 结果 耗时 内存
0 答案正确 3 ms 376KB
1 答案正确 3 ms 404KB
2 答案正确 3 ms 384KB
3 答案正确 5 ms 504KB
4 答案正确 5 ms 384KB
5 答案正确 4 ms 384KB

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

易错点已在代码中标注;

#include <iostream>
#include <algorithm>
#include <cstring>
//易错点:
//1.判断==
//2.既有路径条数又有最大物资时,更新最短路径结构改变
//3.访问过要标注vis[u]=true;
using namespace std;

const int inf=1000000000;
const int maxn=505;
struct Node{
    int data;
    int lenth;
};
vector<Node> Adj[maxn];
int n,num[maxn],weight[maxn],w[maxn];//城市个数,路径条数,每城人数,当前最大人数
int m,c1,c2;//路径条数,起点,终点
int d[maxn];//路径
bool vis[maxn] = {false};

void Dijkstra(int c){
    //初始化
    fill(num,num+maxn,0);
    fill(w,w+maxn,0);
    fill(d,d+maxn,inf);
    num[c]=1;
    w[c]=weight[c];
    d[c]=0;
    //找出当前未遍历的距离最近的点
    for(int i=0; i<n; i++){
        int MIN=inf, u=-1;
        for(int j=0; j<n; j++){
            if(vis[j]==false && MIN>d[j]){
                u=j;
                MIN=d[j];
            }
        }
        if(u==-1) return;
        vis[u]=true;//这里易忘
        //验证是否可以优化
        for(int j=0; j<Adj[u].size(); j++){
            int v=Adj[u][j].data;
            if(vis[v]==false){
                if(d[v]>d[u]+Adj[u][j].lenth){
                    d[v]=d[u]+Adj[u][j].lenth;
                    w[v]=w[u]+weight[v];
                    num[v]=num[u];
                }
                else if(d[v]==d[u]+Adj[u][j].lenth){//这里易错,路径条数与人数无关
                    if(w[v]<w[u]+weight[v]){
                        w[v]=w[u]+weight[v];
                    }
                    num[v]+=num[u];
                }
            }
        }
    }
}

int main()
{
    cin>>n>>m>>c1>>c2;
    for(int i=0; i<n; i++){
        cin>>weight[i];//输入城市人数
    }
    int p1,p2,L;//路径两端及其长度
    Node tmp;
    for(int i=0; i<m; i++){//建图
        cin>>p1>>p2>>L;
        tmp.data=p2;
        tmp.lenth=L;
        Adj[p1].push_back(tmp);
        tmp.data=p1;
        Adj[p2].push_back(tmp);
    }

    Dijkstra(c1);//找出从c1出发所有最短路径

    cout<<num[c2]<<" "<<w[c2];
    return 0;
}

猜你喜欢

转载自blog.csdn.net/Dirichlet_zju/article/details/84585314