admin管理员组

文章数量:1577818

 

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

题目大意是:给你N个城市,这n个城市之间一共有M个边(无向边,一开始没注意到是无向边在pat提交时两个测试点错误,卡了好长时间,一定要细心,一定要细心,一定要细心,重要的事情说三遍),每个城市都有一定数目的救援小组,所有边的边权已知,给定起点和终点,求出最短路径的条数和最短路径上的救援小组数目之和,如果有多条最短路径,则输出救援小组数最多的。这是个典型的单源最短路径问题,中间进行了扩展,读者可以先看上一篇博客再看这一篇。上一篇博客的链接                         https://blog.csdn/qq_38938670/article/details/83446365

 

先贴出我自己的accepted code:

有3点需要说明:

1、此题是求最短路径条数加上第二标尺点权的综合应用。

2、注意顶点下标是从0开始到n-1,并且是无向边。

3、当d[u]+adj[u[[j].dis==d[v]时,无论w[u]+weight[v]>w[v]是否成立,都应当让num[v]+=num[u],因为最短路径条数的依据仅仅是第一标尺距离,与点权无关。

 

#include <iostream>
#include <vector>
#include <string.h>
using namespace std;
#define maxv 510
#define inf 100000000

struct Node{
    int v,dis;
};



int n,m;
bool vis[maxv]={false};
vector<Node> adj[maxv];
int weight[maxv];
int num[maxv];
int d[maxv],w[maxv]={0};




void Dijk(int s)
{
    memset(w,0,sizeof(w));
    fill(d,d+maxv,inf);
    memset(num,0,sizeof(num));

    w[s]=weight[s];
    d[s]=0;
    num[s]=1;
    for(int i=0;i<n;i++)
    {
        int u=-1,mina=inf;
        for(int j=0;j<n;j++)
        {
            if(vis[j]==false&&d[j]<mina)
            {
                mina=d[j];
                u=j;
            }
        }

        if(u==-1) return;
        vis[u]=true;
        for(int j=0;j<adj[u].size();j++)
        {
            int v=adj[u][j].v;
            if(vis[v]==false)
            {
                if(d[u]+adj[u][j].dis<d[v])
                {
                    d[v]=d[u]+adj[u][j].dis;
                    w[v]=w[u]+weight[v];
                    num[v]=num[u];
                }
                else if(d[u]+adj[u][j].dis==d[v])
                {
                    if(w[u]+weight[v]>w[v])
                        w[v]=w[u]+weight[v];

                    num[v]+=num[u];//最短路径的条数和点权无关,所以写在if外面
                }
            }
        }
    }
}




int main()
{
    int a,b,t1,t2,t3;
    cin>>n>>m>>a>>b;
    for(int i=0;i<n;i++)
        cin>>weight[i];

    for(int i=0;i<m;i++)
    {
        cin>>t1>>t2>>t3;
        Node temp,tem;
        temp.v=t2;
        tem.v=t1;
        temp.dis=t3;
        tem.dis=t3;
        adj[t1].push_back(temp);
        adj[t2].push_back(tem);
    }
    Dijk(a);
    cout<<num[b]<<" "<<w[b]<<endl;
    return 0;
}

 

本文标签: 最短路径题目PATEmergency