PAT 1003 Emergency(最短路(迪杰斯特拉||贝尔曼)最小边权下的最大点权)

本文介绍了一种基于图论的算法,用于找到两个城市之间的最短路径,并在此基础上收集尽可能多的救援队伍。采用迪杰斯特拉算法和贝尔曼算法实现,通过实例演示了算法的具体应用。

1003. Emergency (25)

时间限制
400 ms
内存限制
65536 kB
代码长度限制
16000 B
判题程序
Standard
作者
CHEN, Yue

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

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.

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个城市,m条边,每条边有边权,每个城市有点权,最短路可能有多条,要求出路径最短的情况下的点权最大的路径,并输出,还要输出最短路径的条数。

思路:可以用迪杰斯特拉也可以用贝尔曼算法解决。还可以用深搜。

迪杰斯特拉:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
using namespace std;
#define inf 0x3fffffff
const int N=505;
int m,n,c1,c2,e[N][N],dis[N],num[N],pit[N],sum[N];
bool vis[N];
void dijkstra()
{
    fill(dis,dis+N,inf);
    memset(vis,false,sizeof(vis));
    memset(num,0,sizeof(num));//最短路径数量
    memset(sum,0,sizeof(sum));//点权
    dis[c1]=0;
    num[c1]=1;
    sum[c1]=pit[c1];
    for(int i=0;i<n;i++)
    {
        int tmp=inf,u=-1;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&dis[j]<tmp)
            {
                tmp=dis[j];
                u=j;
            }
        }
        if(u==-1) return ;
        vis[u]=true;
        for(int j=0;j<n;j++)
        {
            if(!vis[j]&&e[u][j]!=inf)
            {
                if(dis[u]+e[u][j]<dis[j])
                {
                    dis[j]=dis[u]+e[u][j];
                    num[j]=num[u];
                    sum[j]=sum[u]+pit[j];
                }
                else if(dis[u]+e[u][j]==dis[j])
                {
                    num[j]+=num[u];
                    if(sum[u]+pit[j]>sum[j])
                        sum[j]=sum[u]+pit[j];
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&c1,&c2))
    {
        fill(e[0],e[0]+N*N,inf);
        int a,b,c;
        for(int i=0;i<n;i++)
            scanf("%d",&pit[i]);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&a,&b,&c);
            e[a][b]=e[b][a]=c;
        }
        dijkstra();
        printf("%d %d\n",num[c2],sum[c2]);
    }
}

贝尔曼

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<vector>
#include<set>
using namespace std;
#define inf 0x3fffffff
const int N=510;
struct node
{
    int v,dis;
    node(int _v,int _dis) : v(_v),dis(_dis) {}//构造函数
};
vector<node> adj[N];//邻接表存图
int dis[N],num[N],sum[N],pit[N],m,n,c1,c2;
set<int> pre[N];
void bell()
{
    fill(dis,dis+N,inf);
    memset(num,0,sizeof(num));;
    memset(sum,0,sizeof(sum));
    dis[c1]=0;
    num[c1]=1;
    sum[c1]=pit[c1];
    for(int i=0;i<n-1;i++)
    {
        for(int u=0;u<n;u++)//转折点
        {
            for(int j=0;j<adj[u].size();j++)
            {
                int v=adj[u][j].v;//终点
                int diss=adj[u][j].dis;
                if(dis[u]+diss<dis[v])
                {
                    dis[v]=dis[u]+diss;
                    num[v]=num[u];
                    sum[v]=sum[u]+pit[v];
//                    sum[v]+=pit[u];//这样写也对
                    pre[v].clear();
                    pre[v].insert(u);
                }
                else if(dis[u]+diss==dis[v])
                {
                    if(sum[u]+pit[v]>sum[v])
                        sum[v]=sum[u]+pit[v];
                    pre[v].insert(u);
                    num[v]=0;//遇到相同的最短路,将路径数清零
                    set<int>::iterator it;
                    for(it=pre[v].begin();it!=pre[v].end();it++)
                        num[v]+=num[*it];//遍历前驱结点,将所有前驱结点的路径数量加起来
                }
            }
        }
    }
}
int main()
{
    while(~scanf("%d%d%d%d",&n,&m,&c1,&c2))
    {
        int u,v,wt;
        for(int i=0;i<n;i++)
            scanf("%d",&pit[i]);
        for(int i=0;i<m;i++)
        {
            scanf("%d%d%d",&u,&v,&wt);
            adj[u].push_back(node(v,wt));
            adj[v].push_back(node(u,wt));
        }
        bell();
        printf("%d %d\n",num[c2],sum[c2]);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值