PAT A1003 Emergency

PAT (Advanced Level) Practice

1003 Emergency

分数 25

作者 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 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, 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 Specification:

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

solution one:Dijkstra算法

#include <iostream>
#include <cstring>
#include <algorithm>

using namespace std;

const int INF = 0x3fffffff; //无穷大数据
const int maxn=510; //顶点最大值

int G[maxn][maxn]; //邻接矩阵
int weight[maxn];  //顶点权值

int w[maxn];  // 路径上顶点上筹集的最大物资
int num[maxn]; // 最短路径条数
int d[maxn];  //最短路径
bool hs[maxn]; // 顶点是否被标记

int Nv,Ne,st,ed; //顶点数,边数,起点,终点

void Read(); //读出数据,并建图
void Dijkstra(int s); //Gijkstra 算法

int main()
{
    fill(*G,*G+maxn*maxn,INF);   //这句话没写,害我调试了半天
    Read();
    Dijkstra(st);

    cout << num[ed] << ' ' << w[ed] << endl;

    return 0;
}

void Read()  //读出数据,并建图
{
    cin >> Nv >>Ne >> st >> ed;
    for(int i=0;i<Nv;++i)
        cin >> weight[i];
    for(int i=0;i<Ne;++i)
    {
        int v1,v2,dis;
        cin >> v1 >> v2 >> dis;
        G[v1][v2] = G[v2][v1] = dis;
    }
}

void Dijkstra(int s)  //Gijkstra 算法
{
    fill(d,d+maxn,INF);
    d[s]=0;
    w[s]=weight[s];
    num[s]=1;

    for(int i=0;i<Nv;++i)
    {
        int u=-1,MIN=INF;
        for(int j=0;j<Nv;++j)
        {
            if(hs[j]==0 && d[j]<MIN)
            {
                u=j;
                MIN=d[j];
            }
        }

        if(u==-1)
            return;
        hs[u]=1;

        for(int v=0;v<Nv;++v)
        {
            if(hs[v]==0 && G[u][v]!=INF && d[u]+G[u][v] < d[v])
            {
                d[v]= d[u] +G[u][v];
                w[v]=w[u]+weight[v];
                num[v]=num[u];
            }
            else if(hs[v]==0 && G[u][v]!=INF && d[u]+G[u][v] == d[v])
            {
                if(w[u]+weight[v] > w[v])
                    w[v] = w[u]+weight[v];
                num[v]+=num[u];
            }
        }
    }
}



solution two:Bellman_Ford算法:

#include <iostream>
#include <cstring>
#include <algorithm>
#include <set>
#include <vector>

using namespace std;
 
typedef pair<int, int> PII;

const int INF = 0x3fffffff; //无穷大数据
const int maxn=510; //顶点最大值
 
vector<PII> Adj[maxn]; //邻接表
int weight[maxn];  //顶点权值
 
int w[maxn];  // 路径上顶点上筹集的最大物资
int num[maxn]; // 最短路径条数
int d[maxn];  //最短路径
bool hs[maxn]; // 顶点是否被标记
set<int> pre[maxn];

int Nv,Ne,st,ed; //顶点数,边数,起点,终点
 
void Read(); //读出数据,并建图
void Dijkstra(int s); //Gijkstra 算法
 
int main()
{
    Read();
    Dijkstra(st);
 
    cout << num[ed] << ' ' << w[ed] << endl;
 
    return 0;
}
 
void Read()  //读出数据,并建图
{
    cin >> Nv >>Ne >> st >> ed;
    for(int i=0;i<Nv;++i)
        cin >> weight[i];
    for(int i=0;i<Ne;++i)
    {
        int v1,v2,dis;
        cin >> v1 >> v2 >> dis;
        Adj[v1].push_back({v2,dis});
        Adj[v2].push_back({v1,dis});
    }
}
 
void Dijkstra(int s)  //Gijkstra 算法
{
    fill(d,d+maxn,INF);
    d[s]=0;
    w[s]=weight[s];
    num[s]=1;
    
    for(int i=1;i<=Nv;++i)
    {
        for(int u=0;u<Nv;++u)
        {
            for(int j=0;j<Adj[u].size();++j)
            {
                int v = Adj[u][j].first,dis = Adj[u][j].second;
                if(d[u] + dis < d[v])
                {
                    d[v] = d[u] + dis;
                    num[v] = num[u];
                    w[v] = w[u] + weight[v];
                    
                    pre[v].clear();
                    pre[v].insert(u);
                }
                else if(d[u] + dis == d[v])
                {
                    if(w[u] + weight[v] > w[v])
                        w[v] = w[u] + weight[v];
                    pre[v].insert(u);
                    num[v] = 0;
                    for(auto s:pre[v])
                        num[v] += num[s];
                }
            }
        }
    }
}

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值