码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2016-05-13 04:29:41      阅读:415      评论:0      收藏:0      [点我收藏+]

标签:

【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;
}




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

标签:

原文地址:http://blog.csdn.net/challengerrumble/article/details/51339026

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!