【PAT】Emergency(最短路条数-SPFA)

【PAT】Emergency(最短路条数-SPFA)

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.

输入描述:
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.
输出描述:
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.
输入例子:
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
输出例子:
2 4

题目大意:给一个无向图,求给定两点间最短路的条数。另外每个点有价值,问最短路中价值和最大的一条路的价值和。

对于问题二,普通的最短路算法都可以解决,就不详述了。

问题一是之前我们校赛出过的一个题,当时死活想不出来,昨天看这题想了想就有了初步的思路了。确实虽然没搞多久ACM,但成长确实很大!

对于条数,我的做法是把vis数组换成一个累加器数组cnt

cnt[v] == 0表示到v的最短路为0条,同时,也就说明v未加入到队列。

当从队列中取出一个点u,此时cnt[u]为所有当前到u的最短路的条数。(无法理解的话,先假设是这样,因为对于起点来说,这个肯定是对的吧?cnt[st] = 1

之后,遍历所有点v,如果dis[v] > dis[u]+mp[u][v] 那么直接更新,同时cnt[v] = cnt[u]。因为之前到v的都不再是最短了,那些条数也就无用了。

在cnt[v] = cnt[u]之前,先看cnt[v]是否为0 为0则加入队列。

如果dis[v] == dis[u]+mp[u][v]则说明从u到v是第x条到v的最短路(因为dis[v]为最短,说明之前有一个点`u到v为最短。

此时则把cnt[u]累加到cnt[v]中。同时在这之前也要判断cnt[v],如果为0则加入队列。

最后的最后,对于每个从队列中取出的u,遍历完所有v之后,要把cnt[u]置0! 因为作为最短路,已经把cnt[u]累计到了所有的v中。

最后的最后的最后,如果取出来时v == en(终点) continue

代码如下:

#include <bits/stdc++.h>
#define LL long long
#define Pr pair<int,int>
#define VI vector<int>

using namespace std;
const int INF = 0x3f3f3f3f;
const int mod = 1e9+7;
const double eps = 1e-8;

int mp[555][555];
int head[555];
int cnt[555],dis[2][555];
int val[555];
int st,en,n;

void spfa()
{
    queue <int> q;
    q.push(st);
    memset(cnt,0,sizeof(cnt));
    memset(dis[0],INF,sizeof(int)*555);
    memset(dis[1],0,sizeof(int)*555);

    dis[0][st] = 0;
    dis[1][st] = val[st];
    cnt[st] = 1;
    int u,v,w;
    while(!q.empty())
    {
        u = q.front();
        q.pop();
        if(u == en) continue;

        for(v = 0; v < n; ++v)
        {
            w = mp[u][v];
            if(dis[0][v] > dis[0][u]+w)
            {
                dis[0][v] = dis[0][u]+w;
                dis[1][v] = dis[1][u]+val[v];
                if(!cnt[v]) q.push(v);
                cnt[v] = cnt[u];
            }
            else if(dis[0][v] == dis[0][u]+w)
            {
                dis[1][v] = max(dis[1][u]+val[v],dis[1][v]);
                if(!cnt[v]) q.push(v);
                cnt[v] += cnt[u];
            }
           // printf("%d->%d dis:%d val:%d cnt:%d\n",u,v,dis[0][v],dis[1][v],cnt[v]);
        }
        cnt[u] = 0;
    }
}

int main()
{

    int m,u,v,w;
    scanf("%d%d%d%d",&n,&m,&st,&en);

    for(int i = 0; i < n; ++i) scanf("%d",&val[i]);
    memset(mp,INF,sizeof(mp));

    while(m--)
    {
        scanf("%d%d%d",&u,&v,&w);
        if(mp[u][v] > w) mp[u][v] = mp[v][u] = w;
    }

    spfa();
    printf("%d %d\n",cnt[en],dis[1][en]);

    return 0;
}

时间: 2024-08-29 01:25:01

【PAT】Emergency(最短路条数-SPFA)的相关文章

关于 最短路条数 和 边不可重复最短路条数问题 /hdu3599(边不可重复最短路)

原先一直在做一道省赛题,由于题意错误理解成球最短路条数,误打误撞敲了最短路条数,又发现hdu3599(多校)求边不可重复最短路条数.下面说说俩种问题解法: 最短路条数: 求一个图一共一几条最短路径,思路:先从终点跑一边最短路,记录终点到到所有点最短距离(d[i]),然后从起点出发,dfs 按d[i]走(必是最短路径),一句话:我到终点的最短路条数=我的所有孩子到终点的最短路条数之和,这样只需一遍即可.这不知道是不是叫记忆化搜索? 边不可重复最短路径条数:(最短路+建新图之最大流) hdu3599

codeforces257 div2 D最短路条数

题意: 给一个无向图,总共有 n个点,m+k条边,给定点所连的k条边可以选择删除 问最多删除多少条可以保持该定点到其他点的最短路不变 题解: 从定点出发做单元最短路 首先如果定点到某个点的最短路小于 可删边的长度,则肯定可以删除 此外如果最短路与可删边长度相等,而且最短路条数大于1,肯定也可以删除 所以在做最短路的时候需要记录一下条数 //同时还会有重边,也要注意处理 ps...这题sxbk的把普通的spfa 都卡了..加了slf优化才过,据说dij可以轻松过..不过我没试2333 以后写spf

POJ 3463 最(次)短路条数

Sightseeing Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 9497   Accepted: 3340 Description Tour operator Your Personal Holiday organises guided bus trips across the Benelux. Every day the bus moves from one city S to another city F. O

PAT乙级:1057 数零壹 (20分)

PAT乙级:1057 数零壹 (20分) 题干 给定一串长度不超过 105 的字符串,本题要求你将其中所有英文字母的序号(字母 a-z 对应序号 1-26,不分大小写)相加,得到整数 N,然后再分析一下 N 的二进制表示中有多少 0.多少 1.例如给定字符串 PAT (Basic),其字母序号之和为:16+1+20+2+1+19+9+3=71,而 71 的二进制是 1000111,即有 3 个 0.4 个 1. 输入格式: 输入在一行中给出长度不超过 105.以回车结束的字符串. 输出格式: 在

PAT 乙级 1060 爱丁顿数(25) C++版

1060. 爱丁顿数(25) 时间限制 250 ms 内存限制 65536 kB 代码长度限制 8000 B 判题程序 Standard 作者 CHEN, Yue 英国天文学家爱丁顿很喜欢骑车.据说他为了炫耀自己的骑车功力,还定义了一个"爱丁顿数"E,即满足有E天骑车超过E英里的最大整数E.据说爱丁顿自己的E等于87. 现给定某人N天的骑车距离,请你算出对应的爱丁顿数E(<=N). 输入格式: 输入第一行给出一个正整数N(<=105),即连续骑车的天数:第二行给出N个非负整

PAT试题 自守数(Java编写)

import java.util.Scanner; public class selfConservation { public static void main(String[] args) { Scanner scan = new Scanner(System.in); int M = scan.nextInt(); int K[] = new int[M]; for (int i = 0; i < M; i++) { K[i] = scan.nextInt(); } /*for(int i

HDU1688 Sightseeing(SPFA 求最短路与次短路的路径条数)可用作模板

Sightseeing Time Limit: 3000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 718    Accepted Submission(s): 293 Problem Description Tour operator Your Personal Holiday organises guided bus trips across the Bene

PAT Advanced level 1003 Emergency

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

1003 Emergency

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