PAT 1003 Emergency(SPFA+dfs回溯记录最短路径条数)

PAT1003

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

一开始读错题了,以为求的是最短路+在最短路上的最大花费。SPFA+贪心WA了

后来发现是统计最短路的条数,题意也没说最短路的条数里点是否能重合(这就是两个算法了),就写了一个回溯过了。

 

思路:先用SPFA算出最短路,再DFS进行回溯统计最短路的条数,同时记录出路径当中的最大花费。

 

#include
#include
#include
#include
#include
#include
#define memset(a,v) memset(a,v,sizeof(a))
using namespace std;
const int MAXN(1000);
const int INF(0x3f3f3f3f);
typedef long long int LL;
struct node {
    int to,next,w;
    node() {}
    node(int to,int next,int w):to(to),next(next),w(w) {}
}edge[2*MAXN+50];
int head[2*MAXN+50];
int cost[MAXN+50];
int dis[MAXN+50];
int vis[MAXN+50];
int n,m,sc,st,cnt,shortest_path;
int pathCnt,maxCost;
void SPFA() {
    memset(dis,INF);
    memset(vis,0);
    queueq;
    dis[sc]=0;
    vis[sc]=1;
    q.push(sc);
    while(!q.empty()) {
        int u=q.front();
        q.pop();vis[u]=0;
        for(int i=head[u];~i;i=edge[i].next) {
            int v=edge[i].to,w=edge[i].w;
            if(dis[v]>dis[u]+w) {
                dis[v]=dis[u]+w;
                if(!vis[v])
                    q.push(v),vis[v]=1;
            }
        }
    }
}
void addedge(int u,int v,int w) {
    edge[cnt]=node(v,head[u],w);
    head[u]=cnt++;
}
void init() {
    cnt=pathCnt=maxCost=0;
    memset(head,-1);
}
void dfs(int u,int path,int c) {
    if(u==st) {
        if(path==shortest_path){
            pathCnt++;
            maxCost=max(maxCost,c);
        }
        return;
    }
    for(int i=head[u];~i;i=edge[i].next) {
        int v=edge[i].to,w=edge[i].w;
        if(!vis[v]) {
            vis[v]=1;
            dfs(v,path+w,c+cost[v]);
            vis[v]=0;
        }
    }
}
int main() {
    while(~scanf("%d%d%d%d",&n,&m,&sc,&st)) {
        init();
        for(int i=0;i

 

你可能感兴趣的:(图论,PAT)