PAT(Advanced Level) 1003. Emergency(25) 最短路 + DFS

题目链接

Emergency

Time limit:1 seconds Memory limit:256 megabytes


Problem Description

IAs 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

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, C1 and C2 - 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 c1, c2 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 C1 to C2.

Output

For each test case, print in one line two numbers: the number of different shortest paths between C1 and C2, 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.

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

Output

2 4


题意:

求最短路的条数,以及最短路中的最大权

解题思路:

直接SPFA或Dijistra得出最短路,然后深搜一遍找最大权

Code:

#include <cstdio>
#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <queue>

using namespace std;

const int maxn=500+5,maxm=500000;
const int INF = 0x3f3f3f3f;
struct EdgeNode
{
    int to,w,next;
};

EdgeNode edges[maxm];
int N,M;
int head[maxn],edge;
bool vis[maxn];
queue<int> que;
int dis[maxn];
int C1, C2;
int team[maxn];
int ans_len=0, ans_team=0,cnt=0;

void addedge(int u,int v,int c)
{
    edges[edge].w=c,edges[edge].to=v,edges[edge].next=head[u],head[u]=edge++;
}

void init()
{
    memset(head,-1,sizeof(head));
    edge=0;
}

void spfa(int s,int n)
{
    int u;
    for(int i=0;i<n;i++)
        dis[i]=INF;
    while(!que.empty())que.pop();
    que.push(s);
    vis[s]=true;
    dis[s]=0;
    while(!que.empty())
    {
        u = que.front();
        que.pop();
        vis[u] = false;
        for (int i = head[u]; i !=-1; i=edges[i].next)
        {
            int v = edges[i].to;
            int w = edges[i].w;
            if (dis[v]>dis[u]+w)
            {
                dis[v] = dis[u] + w;
                if(!vis[v])
                {
                    vis[v] = true;
                    que.push(v);
                }
            }
        }
    }
}

void DFS(int u,int teams,int len)
{
    //printf("%d %d %d\n", u, teams, len);
    if (u == C2 && ans_team < teams && len == ans_len)
    {
        ans_team = teams;
    }
    if (u == C2 &&len == ans_len)
    {
        cnt++;
    }
    if(len>=ans_len)
    {
        return;
    }
    vis[u] = true;
    for (int i = head[u]; i !=-1; i=edges[i].next)
    {
        int v = edges[i].to;
        int w = edges[i].w;
        if (!vis[v]&&len+w<=ans_len)
        {
            vis[v] = true;
            DFS(v, teams + team[v],len+w);
            vis[v] = false;
        }
    }
}


int main()
{
    init();
    cin >> N >> M >> C1 >> C2;
    for (int i = 0; i < N; i++)
    {
        cin >> team[i];
    }
    for (int i = 0; i < M;i++)
    {
        int u, v, c;
        cin >> u >> v >> c;
        addedge(u, v, c);
        addedge(v, u, c);
    }
    spfa(C1, N);
    ans_len = dis[C2];
    memset(vis, false, sizeof(vis));
    DFS(C1, team[C1], 0);
    cout << cnt << ' ' << ans_team << endl;
    return 0;
}

猜你喜欢

转载自blog.csdn.net/xp731574722/article/details/79478564