首页 > 其他分享 >24/02/04 CF567E President and Roads

24/02/04 CF567E President and Roads

时间:2024-02-04 22:11:51浏览次数:29  
标签:24 02 CF567E int roads along op road dis

题目描述

Berland has $ n $ cities, the capital is located in city $ s $ , and the historic home town of the President is in city $ t $ ( $ s≠t $ ). The cities are connected by one-way roads, the travel time for each of the road is a positive integer.

Once a year the President visited his historic home town $ t $ , for which his motorcade passes along some path from $ s $ to $ t $ (he always returns on a personal plane). Since the president is a very busy man, he always chooses the path from $ s $ to $ t $ , along which he will travel the fastest.

The ministry of Roads and Railways wants to learn for each of the road: whether the President will definitely pass through it during his travels, and if not, whether it is possible to repair it so that it would definitely be included in the shortest path from the capital to the historic home town of the President. Obviously, the road can not be repaired so that the travel time on it was less than one. The ministry of Berland, like any other, is interested in maintaining the budget, so it wants to know the minimum cost of repairing the road. Also, it is very fond of accuracy, so it repairs the roads so that the travel time on them is always a positive integer.

输入格式

The first lines contain four integers $ n $ , $ m $ , $ s $ and $ t $ ( $ 2<=n<=10^{5}; 1<=m<=10^{5}; 1<=s,t<=n $ ) — the number of cities and roads in Berland, the numbers of the capital and of the Presidents' home town ( $ s≠t $ ).

Next $ m $ lines contain the roads. Each road is given as a group of three integers $ a_{i},b_{i},l_{i} $ ( $ 1<=a_{i},b_{i}<=n; a_{i}≠b_{i}; 1<=l_{i}<=10^{6} $ ) — the cities that are connected by the $ i $ -th road and the time needed to ride along it. The road is directed from city $ a_{i} $ to city $ b_{i} $ .

The cities are numbered from 1 to $ n $ . Each pair of cities can have multiple roads between them. It is guaranteed that there is a path from $ s $ to $ t $ along the roads.

输出格式

Print $ m $ lines. The $ i $ -th line should contain information about the $ i $ -th road (the roads are numbered in the order of appearance in the input).

If the president will definitely ride along it during his travels, the line must contain a single word "YES" (without the quotes).

Otherwise, if the $ i $ -th road can be repaired so that the travel time on it remains positive and then president will definitely ride along it, print space-separated word "CAN" (without the quotes), and the minimum cost of repairing.

If we can't make the road be such that president will definitely ride along it, print "NO" (without the quotes).

提示

The cost of repairing the road is the difference between the time needed to ride along it before and after the repairing.

题意简述

总统要回家,即从 \(s\) 走到 \(t\),会经过一些街道,每条街道都是单向的并且拥有权值。现在,为了让总统更好的回家,要对每一条街道进行操作:

  • 如果该街道一定在最短路上,则输出“YES” 。

  • 如果该街道修理过后,该边所在的最短路可以取代原先的最短路,则输出“CAN \(x\)” , \(x\) 是修改该街道的最小花费,就是权值减小的值。

  • 如果该街道不在 \(s\) 到 \(t\) 的路径上,或者修改过后权值小于等于0,则输出“NO”。


最短路好题,*2200 刚刚好,就是写起来有点折磨人。

如何确定一条边是否必经?

image

正反图两遍统计一下最短路的数量。

设 \(s \rightarrow x\) 的方案有 \(c_1\) 种,\(y \rightarrow t\) 的方案有 \(c_2\) 种,设 \(s \rightarrow t\) 的方案有 \(c\) 种,

如果一条边必经,那么必然满足 \(c_1 \times c_2 = c\)。

然而方案数可能很大,于是需要 hash。

为了保证答案是对的,还要验证一下 \(dis_{s,x} + l_{x,y} + dis_{y,t}\) 是否等于 \(dis_{s,t}\) 。

一般来说最好写双哈,但这题放了单 hash,于是偷了懒daze。

CAN 和 NO 与 YES 类似,看看要减多少才能成为最短路的长度,然后再减一使最短路强制经过。

如果边权减没了,输出 NO 就好了


代码实现

正图反图真麻烦啊。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define mod 998244353ll
using namespace std;
using ll=long long;
const int N=1e5+5,M=1e5+5;
const ll inf=1e16+5;
int n,m,s,t;
struct graph{
    struct edge{int to,next,val;}e[M<<1];
    int head[N],idx=1;
    void init(){memset(head,0,sizeof(head));}
    inline void add(int from,int to,int val){
        e[++idx].to=to;
        e[idx].val=val;
        e[idx].next=head[from];
        head[from]=idx;
    }
}S[2];
struct edge{int from,to,val;}e[M<<1];
struct node{
    int pos;ll val;
    friend bool operator<(node x,node y){
        return x.val>y.val;
    }
};
priority_queue<node>q;
ll dis[N][2],cnt[N][2];
bool vis[N];
void dijkstra(int st,int op){
    for(int i=1;i<=n;i++){
        dis[i][op]=inf,cnt[i][op]=0;
        vis[i]=0;
    }
    q.push((node){st,0});
    dis[st][op]=0,cnt[st][op]=1;
    while(q.size()){
        int p=q.top().pos;
        q.pop();
        if(vis[p])continue;
        vis[p]=1;
        for(int i=S[op].head[p];i;i=S[op].e[i].next){
            int to=S[op].e[i].to;
            if(dis[to][op]>dis[p][op]+S[op].e[i].val){
                dis[to][op]=dis[p][op]+S[op].e[i].val;
                cnt[to][op]=cnt[p][op];
                q.push((node){to,dis[to][op]});
            }
            else if(dis[to][op]==dis[p][op]+S[op].e[i].val){
                (cnt[to][op]+=cnt[p][op])%=mod;
            }
        }
    }
}
int main(){
    ios::sync_with_stdio(0);
    cin.tie(0);
    cin>>n>>m>>s>>t;
    S[0].init(),S[1].init();
    for(int i=1,x,y,v;i<=m;i++){
        cin>>x>>y>>v;
        e[i]=(edge){x,y,v};
        S[0].add(x,y,v),S[1].add(y,x,v);
    }
    dijkstra(s,0);dijkstra(t,1);
    for(int i=1;i<=m;i++){
        int x=e[i].from,y=e[i].to,v=e[i].val;
        if(dis[x][0]+v+dis[y][1]==dis[t][0]&&cnt[x][0]*cnt[y][1]%mod==cnt[t][0]){
            cout<<"YES"<<'\n';
        }
        else {
            ll k=dis[t][0]-dis[x][0]-dis[y][1];
            if(k<=1)cout<<"NO"<<'\n';
            else cout<<"CAN "<<v-k+1<<'\n';
        }
    }
    return 0;
}
//iictiw-marisa

标签:24,02,CF567E,int,roads,along,op,road,dis
From: https://www.cnblogs.com/Iictiw/p/18007098

相关文章

  • .NET周刊【1月第3期 2024-01-24】
    国内文章.NET开源的简单、快速、强大的前后端分离后台权限管理系统https://www.cnblogs.com/Can-daydayup/p/17980851本文介绍了中台Admin,一款基于Vue3和.NET8的开源后台权限管理系统。它具备前后端分离架构,支持多租户、接口和数据权限、动态Api等功能,并集成了多种中间件和服务......
  • 2024.2.4寒假每日总结26
    算法题:292.Nim游戏-力扣(LeetCode)LeetCodeNim游戏292.Nim游戏-力扣(LeetCode)题目描述你和你的朋友,两个人一起玩Nim游戏:桌子上有一堆石头。你们轮流进行自己的回合,你作为先手。每一回合,轮到的人拿掉1-3块石头。拿掉最后一块石头的人就是获胜者。......
  • 提前祝大家新年好!来看看社区 2023 都得了哪些奖吧~
    大噶好!转眼马上就是“龙”历新年啦,不知道大家这周的工作热情怎么样呢?小陈的心已经在殷切期盼回家过年了~ RTE开发者社区预祝诸位: 2024年......
  • A027 《满天星》编程 源码
    一、课程介绍本节课学习函数定义与使用,并借此绘制出漫天星光。二、重难点解析函数的定义和调用对函数的理解:把实现某种功能的多行代码包装成一个函数,并取好函数名。后面可以通过调用这个函数实现相应的功能,从而实现简化代码的效果、复用的作用。我们平常使用的方法如forward()等......
  • How to unlock Nissan Altima 2019-2022 Smart Remote 5 Buttons 433MHz Keys with Sm
    Howtounlock Nissan Altima2019-2022Smart Remote 5Buttons433MHzKeyswithSmartPro5000U-Plusfirst,youneedhavea SmartPro5000U-PlusProgrammer,ifyoudonothaveaSmartPro5000U-Plus,youcanbuyonefromchinaobd2.com.https://www.chinaobd2.co......
  • winter 2024 第二周周报
    内容winterweek2day1这套题复习了最短路,主要是dp,都是比较好推的dp,还是要多写dp吧,感觉写dp用的时间太久了day2这天是ccf的测试赛,测完就练了套河南大学联赛,10题看当时榜可能第八,只能说队友太给力了。写的那道l感觉挺好想的求方案数,刚开始也是在猜结论,没有想着去好好推qwq,后面......
  • 2024牛客寒假算法基础集训营1 K 牛镇公务员考试 题解
    Question2024牛客寒假算法基础集训营1K牛镇公务员考试给出一张试卷有\(n\)道单选题,每道题用一个整数\(a_i\)和一个长度为\(5\)的字符串\(s_i\)表示,其中第\(i\)道题的题面为:第\(a_i\)道题的答案是()A.\(s_1\)B.\(s_2\)C.\(s_3\)D.\(s_4\)E.\(s_5\)问:正......
  • 2019-2020 ICPC Northwestern European Regional Programming Contest (NWERC 2019)
    Preface由于前两天疑似感染风寒,今天头痛+鼻塞+咽炎一条龙,硬打了4h顶不住就下班了最后过了8个题也还行,比较可惜的就是H题从中期写到结束,祁神和徐神各写了一个version也没过A.AverageRank挺有意思的一个题考虑将一个原来分数为\(x\)的人加\(1\)分后会发生什么,显然只有原来分......
  • 2022河南萌新联赛第(三)场:河南大学
    A-玉米大炮二分一个时间,然后计算每门大炮可以射击的次数#include<bits/stdc++.h>usingnamespacestd;#defineintlonglong//#defineint__int128#definedoublelongdoubletypedefpair<int,int>PII;typedefpair<string,int>PSI;typedefpair<string,string>PS......
  • 杂记 2024-02-04 农历腊月25,立春
    1.2024-02-04立春。立春,为二十四节气之首。立,是“开始”之意;春,代表着温暖、生长。时至立春,在我国的北回归线(黄赤交角)及其以南一带,可明显感觉到早春的气息。北回归线是一条重要纬线,其自西向东穿过中国云南、广西、广东、福建(海域)、台湾五省区。《立春》宋·白玉蟾东风吹散梅梢雪......