PAT Advanced level 1003 Emergency

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 (≤) - the number of cities (and the cities are numbered from 0 to N−1), M - the number of roads, C?1?? and C?2?? - 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 c?1??, c?2?? 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 C?1?? to C?2??.

Output Specification:

For each test case, print in one line two numbers: the number of different shortest paths between C?1?? and C?2??, 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


这。。。前一题还输入输出呢怎么突然就最短路了。。。完全不按套路出牌啊喂(在最短路基础上额外维护一下题目要求的最短路条数和最大人数就行了

……话是这么说交上去一直WA。。。后来翻网上题解找到了一组把我的程序卡掉的数据之前的WA版本是【如果松弛后的dis和目前的dis一样,也要再入队一次】因为觉得虽然距离没变但是路径数和最大人数都需要更新然而事实上这个点之前已经有过一次入队且未处理的记录了也就是说,假设1、2点到3点的距离一样,先通过1更新3,再通过2更新3的时候,用1更新那时入队的节点还没被处理如果这时候把3再入队一次,3后边的节点就会受到两次3的影响就WA了。

改AC的代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<queue>
const int N=509;
using namespace std;
int n,m,c1,c2;
int rt[N];
int p[N],cnt;
struct edge{int to,nex,val;}e[5000000];
void add(int u,int v,int val){
    e[++cnt]=(edge){v,p[u],val};
    p[u]=cnt;
}
int path_num[N],maxrt[N];
long long dis[N];
struct node{int id;long long dis;};
bool operator <(node x,node y){return x.dis>y.dis;}
bool vis[N];
priority_queue<node>q;
inline void dijkstra(int s)
{
    dis[s]=0;
    while(!q.empty())q.pop();
    q.push((node){s,0});
    while(!q.empty())
    {
      node cur=q.top();q.pop();
      if(cur.dis!=dis[cur.id])continue;
      int u=cur.id;
      for(int k=p[u];k;k=e[k].nex)
      {
        int v=e[k].to;
        if(dis[v]>=dis[u]+e[k].val)
        {
            if(dis[v]==dis[u]+e[k].val){
                path_num[v]+=path_num[u];
                maxrt[v]=max(maxrt[v],maxrt[u]+rt[v]);
            }
            else{
                path_num[v]=path_num[u];
                dis[v]=dis[u]+e[k].val;
                maxrt[v]=maxrt[u]+rt[v];
                q.push((node){v,dis[v]});
            }
        }
      }
    }
}
int main(){
    scanf("%d%d%d%d",&n,&m,&c1,&c2);
    for(int i=0;i<n;++i)scanf("%d",&rt[i]);
    for(int i=1;i<=m;++i){
        int u,v,w;scanf("%d%d%d",&u,&v,&w);
        add(u,v,w);
        add(v,u,w);
    }
    memset(dis,0x3f,sizeof(dis));
    path_num[c1]=1;
    maxrt[c1]=rt[c1];
    dijkstra(c1);
    printf("%d %d",path_num[c2],maxrt[c2]);
    return 0;
} 


原文地址:https://www.cnblogs.com/wypx/p/12188336.html

时间: 2024-11-03 10:57:16

PAT Advanced level 1003 Emergency的相关文章

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

Pat(Advanced Level)Practice--1043(Is It a Binary Search Tree)

Pat1043代码 题目描述: A Binary Search Tree (BST) is recursively defined as a binary tree which has the following properties: The left subtree of a node contains only nodes with keys less than the node's key. The right subtree of a node contains only nodes

Pat(Advanced Level)Practice--1044(Shopping in Mars)

Pat1044代码 题目描述: Shopping in Mars is quite a different experience. The Mars people pay by chained diamonds. Each diamond has a value (in Mars dollars M$). When making the payment, the chain can be cut at any position for only once and some of the diam

PAT (Advanced Level) 1093. Count PAT&#39;s (25)

预处理每个位置之前有多少个P,每个位置之后有多少个T. 对于每个A,贡献的答案是这个A之前的P个数*这个A之后T个数. #include<cstdio> #include<cstring> long long MOD=1e9+7; const int maxn=1e5+10; long long dp1[maxn],dp2[maxn]; char s[maxn]; int main() { scanf("%s",s); memset(dp1,0,sizeof d

PAT (Advanced Level) 1055. The World&#39;s Richest (25)

排序.随便加点优化就能过. #include<iostream> #include<cstring> #include<cmath> #include<algorithm> #include<cstdio> #include<map> #include<queue> #include<string> #include<stack> #include<vector> using names

Pat(Advanced Level)Practice--1018(Public Bike Management)

Pat1018代码 题目描述: There is a public bike service in Hangzhou City which provides great convenience to the tourists from all over the world. One may rent a bike at any station and return it to any other stations in the city. The Public Bike Management C

Pat(Advanced Level)Practice--1076(Forwards on Weibo)

Pat1076代码 题目描述: Weibo is known as the Chinese version of Twitter. One user on Weibo may have many followers, and may follow many other users as well. Hence a social network is formed with followers relations. When a user makes a post on Weibo, all hi

Pat(Advanced Level)Practice--1016(Phone Bills)

Pat1016代码 题目描述: A long-distance telephone company charges its customers by the following rules: Making a long-distance call costs a certain amount per minute, depending on the time of day when the call is made. When a customer starts connecting a lon

1093. Count PAT&#39;s (25)【计数】——PAT (Advanced Level) Practise

题目信息 1093. Count PAT's (25) 时间限制120 ms 内存限制65536 kB 代码长度限制16000 B The string APPAPT contains two PAT's as substrings. The first one is formed by the 2nd, the 4th, and the 6th characters, and the second one is formed by the 3rd, the 4th, and the 6th c