PAT甲级-1003. Emergency (25)多条最短路

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
第一行给出城镇数目、道路数目、起点、终点;

第二行给出各个城镇救援队的数量;

以下各行给出两个城镇之间的距离;

沿路可以收集经过城镇的救援队,计算出起终点之间不同的最短路的数量和最大的救援队数量。

直捣黄龙 http://blog.csdn.net/mikasa3/article/details/65448765 的代码稍微改改过的…
#include<bits/stdc++.h>
using namespace std;
#define INF 0xfffffff
#define MAXN 1010
int st,en;
struct Node
{
    int dist;//最短路长度
    int cnt;//援助数
} a[MAXN];//对于每个城镇
int n,m;//顶点数和边数
bool used[MAXN];//是否使用过
int dis[MAXN];//最短路的数量
int pre[MAXN];//pre[i]表示v0到vi的最短路径上vi的前一个顶点的序号
int num[MAXN];//援助数
int cost[MAXN][MAXN];//边权

void dijkstra(int s)//s到其它点的最短路
{
    for(int i=0; i<=n; ++i)//初始化
    {
        a[i].dist=INF;
        a[i].cnt=0;
        dis[i]=0;
        used[i]=false;
        pre[i]=-1;
    }
    a[s].cnt=num[s];
    a[s].dist=0;//起始点的最短路是0
    dis[s]=1;//起始点的最短路数是1
    while(true)
    {
        int v=-1,minx=INF;
        for(int u=0; u<n; u++)//从未使用过的顶点中选择一个距离最小的顶点
            if(!used[u]&&minx>a[u].dist)
            {
                minx=a[u].dist;
                v=u;
            }
        if(v==-1) break;
        used[v]=true;
        for(int u=0; u<n; ++u)
        {
            if(used[u]) continue;
            if(a[v].dist+cost[v][u]<a[u].dist)//路径更多
            {
                a[u].dist=a[v].dist+cost[v][u];//更新最短路
                a[u].cnt=a[v].cnt+num[u];//加入在当前城镇的援助数更新
                pre[u]=v;//记录路径
                dis[u]=dis[v];//最短路数量
            }
            else if(cost[v][u]!=INF&&cost[v][u]+a[v].dist==a[u].dist)
            {
                dis[u]+=dis[v];//更新最短路数量
                if(a[u].cnt<a[v].cnt+num[u])//援助数更多
                {
                    a[u].dist=cost[v][u]+a[v].dist;
                    a[u].cnt=a[v].cnt+num[u];
                    pre[u]=v;
                }
            }
        }
    }
}

vector<int> get_path(int t)//输出路径,计算到顶点n的最短路
{
    vector<int> path;
    for(; t!=-1; t=pre[t])
        path.push_back(t);
    reverse(path.begin(),path.end());
    return path;
}
int main()
{
#ifdef ONLINE_JUDGE
#else
    freopen("F:/cb/read.txt","r",stdin);
//freopen("F:/cb/out.txt","w",stdout);
#endif
    ios::sync_with_stdio(false);
    cin.tie(0);
    int i;
    cin>>n>>m;
    cin>>st>>en;//起终点城镇名
    for(i=0; i<n; ++i)
        cin>>num[i];//记录援助数量
    for(int i=0; i<=n; ++i)//初始化
        for(int j=0; j<=n; ++j)
            cost[i][j]=INF;
    for(i=0; i<m; i++)
    {
        int s,ss;
        int t;
        cin>>s>>ss>>t;
        cost[s][ss]=t;//无向图
        cost[ss][s]=t;
    }
    dijkstra(st);
    //路径还原,输出路径
    /*vector<int> path;
    path=get_path(en);//输出路径
    int len=path.size();
    for(int i=0; i<len-1; ++i)
        cout<<path[i]<<"->";
    cout<<path[len-1]<<endl;*/
    cout<<dis[en]<<" "<<a[en].cnt<<endl;//输出最短路径的条数、援助总数
    return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值