【PAT甲级】1003 Emergency (25分)

1003 Emergency (25分)

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


分析(参考《算法笔记》p367-p381)

要求:最短路径,最大点权

求:最短路径的条数,最短路径的基础上的最大点权


顶点:城市

边:路

边权:路的长度

点权:每个城市的救援队个数


理解

  • dis[u] 为起点s到点u的最短距离

dis[]数组的初始不需要另外写,第一次循环找到的最小dis[u]一定是dis[s](=0),而后优化所有从s出发能到达的顶点的dis[],即对整个dis[]进行了初始化。


代码

/*邻接表实现Dijkstra算法*/

#include<iostream>
#include<vector>

using namespace std;

const int maxN=500;             //最多顶点个数
const int INF=1000000000;//10^9 dis[u]的最大距离
struct Node {
    int v,dis;//v顶点编号 ,dis 点Adj[u]到点j的边权
};
vector<Node> Adj[maxN];//图G使用邻接表实现,存放从点u出发可以到达的所有顶点
int dis[maxN];//dis[u] 起点s到点u的最短距离
int num[maxN];//num[u] 起点s到u的最短路径条数
int w[maxN];//w[u] 起点s到点u的最大点权
int  weight[maxN];//weight[u] 点u的点权
bool vis[maxN]= {false}; //点的访问数组
int N,M,C1,C2;//城市个数,路的条数,起始城市,目的城市

void Dijkstra(int s) {
    /*初始化*/
    fill(dis,dis+N,INF);//赋值dis数组全部元素为INF
    fill(w,w+N,0);//赋值w数组全部元素为0
    fill(num,num+N,0);// 赋值num数组全部元素为0
    dis[s]=0;//s到自身的距离为 0
    w[s]=weight[s]; //s到自身的点权为 weight[s]
    num[s]=1;//s到自身的最短路径为1条

    /**/
    for(int i=0; i<N; i++) { //循环n次
        //找到 未访问过且最小dis[u]的顶点
        int u=-1;
        int minDis=INF;
        for(int j=0; j<N; j++) {
            if(dis[j]<minDis) {
                u=j;
                minDis=dis[j];
            }
        }

        if(u==-1) return;//没有与s邻接的点

        vis[u]=true;
        for(int v=0; v<N; v++) { //从u出发能到达的所有顶点
            //v未被访问&& u能到达v
            if(vis[v]==false&&Adj[u][v].dis!=INF) {
                if(dis[u]+Adj[u][v].dis<dis[v]) {//以u为中介dis[v]更优
                    dis[v]= dis[u]+Adj[u][v].dis;//优化dis[v]
                    w[v]=w[u]+weight[v];//覆盖w[v]
                    num[v]=num[u];//覆盖 num[v]
                } else if(dis[u]+Adj[u][v].dis==dis[v]) {//找到一条相同长度的路径
                    if(w[u]+weight[v]>w[v]) {//以u为中介时w[v]更大
                        w[v]=w[u]+weight[v];//w[v]继承w[u]
                    }
                    num[v]+=num[u];
                }
            }
        }
    }
}

int main() {
    scanf("%d%d%d%d",&N,&M,&C1,&C2);
    for(int i=0; i<N; i++) {   //点权
        scanf("%d",&weight[i]);
    }
    //边权
    int u,v,t;
    for(int i=0; i<M; i++) {
        scanf("%d%d%d",&u,&v,&t);
        Adj[u].push_back({v,t}); //声明结构体变量{v2,t}
    }

    Dijkstra(C1);
    printf("%d %d",num[C2],w[C2]);

    return 0;
}


注意


BUG

  • 出现这个报错可能是:全局变量和局部变量命名有相同的,使用时误用了另一个变量,改名即可。

    全局变量和局部变量同名时,采用的是局部变量。

局部:

全局:


单词

  • subscript :下标

原文地址:https://www.cnblogs.com/musecho/p/12252135.html

时间: 2024-11-05 14:54:12

【PAT甲级】1003 Emergency (25分)的相关文章

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

PAT 甲级 1029 Median (25 分)(思维题,找两个队列的中位数,没想到)*

1029 Median (25 分) Given an increasing sequence S of N integers, the median is the number at the middle position. For example, the median of S1 = { 11, 12, 13, 14 } is 12, and the median of S2 = { 9, 10, 15, 16, 17 } is 15. The median of two sequence

PAT 甲级 1003 Emergency

1 #include <iostream> 2 #include <cstdio> 3 #include <cstring> 4 #include <cstdlib> 5 #include <algorithm> 6 7 using namespace std; 8 typedef long long ll; 9 const int inf=0x3f3f3f3f; 10 const int o=505; 11 bool vis[o]; 12 in

PAT:1003. Emergency (25) AC

#include<stdio.h> #include<string.h> #include<algorithm> using namespace std; const int MAXV=510; const int INF=0x3fffffff; int n,m,c1,c2; bool vis[MAXV]; int G[MAXV][MAXV]; //城市间距离 int weight[MAXV]; //每个城市的救援人数 int d[MAXV]; //最短距离 int w

PAT 1003 Emergency (25)(25 分)

1003 Emergency (25)(25 分) 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 betwe

1003. Emergency (25)——PAT (Advanced Level) Practise

题目信息: 1003. Emergency (25) 时间限制 400 ms 内存限制 32000 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.

PAT 1003. Emergency (25)

1003. Emergency (25) 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 an

甲级1003 Emergency

1003 Emergency (25 分) 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 a

PAT (Advanced Level) 1003. Emergency (25)

最短路+dfs 先找出可能在最短路上的边,这些边会构成一个DAG,然后在这个DAG上dfs一次就可以得到两个答案了. 也可以对DAG进行拓扑排序,然后DP求解. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<queue> #include<vector> using nam