codeforces567E. President and Roads
题目大意:总统要回家,会经过一些街道,每条街道都是单向的并且拥有权值。现在,为了让总统更好的回家,要对每一条街道进行操作:1)如果该街道一定在最短路上,则输出“YES”。2)如果该街道修理过后,该边所在的最短路可以取代原先的最短路,则输出“CAN x”,x是修改该街道的花费,就是权值减小的值。3)如果该街道是一条不连通的街道,或者修改过后权值小于等于0,则输出“NO”。
解题思路:正向取边,求一次最短路得到d1[], 然后反向取边,再求一次最短路得到d2[]。接着开始判断每一条边是否在最短路上。初始最短路为Min,当d1[u]+d2[v]+dis==Min时,先判断该边是否一定为最短路上的边,是的话输出“YES”,否则在判断该边的权值是否大于1,是的话输出“CAN 1”,否则输出“NO”。当d1[u]+d2[v]+dis>Min时,先判断两段路的差值加一是否会大于等于该边的差值,是的话,输出“NO”,否则输出“CAN 差值加一”,其余情况均输出“NO”。还有一个问题,如何判断边是否一定在最短路上。先把最短路取出,重新建一张无向图,然后用tarjan算法求出哪些边是桥。桥就是一定在最短路上的边。
#include<cstdio>
#include<iostream>
#include<cmath>
#include<cstring>
#include<algorithm>
#define maxn 200005
#define maxm 200005
#define inf 4557430888798830399LL
using namespace std;
typedef long long int64;
char ch;
int n,m,s,t,a[maxm],b[maxm],idx,dfn[maxn],low[maxn];
int64 c[maxm],min_road;
int siz,pos[maxn];
struct Heap{
int64 val;
int num;
bool operator <(const Heap b)const{return val<b.val;}
bool operator >(const Heap b)const{return b<*this;}
bool operator <=(const Heap b)const{return !(*this>b);}
bool operator >=(const Heap b)const{return !(*this<b);}
bool operator ==(const Heap b)const{return (*this<=b)&&(*this>=b);}
bool operator !=(const Heap b)const{return !(*this==b);}
}heap[maxn],tmp;
struct Map{
int tot,now[maxn],son[maxm],pre[maxm],val[maxm];
int64 dist[maxn];
void put(int a,int b,int c){pre[++tot]=now[a],now[a]=tot,son[tot]=b,val[tot]=c;}
}map[];
bool ok,bo[maxn],road[maxm];
void read(int &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void read(int64 &x){
for (ok=,ch=getchar();!isdigit(ch);ch=getchar()) if (ch=='-') ok=;
for (x=;isdigit(ch);x=x*+ch-'',ch=getchar());
if (ok) x=-x;
}
void heap_swap(Heap &a,Heap &b){swap(pos[a.num],pos[b.num]),swap(a,b);}
void up(int son){
int fa=son>>;
while (fa){
if (heap[fa]<=heap[son]) break;
heap_swap(heap[fa],heap[son]);
son=fa,fa>>=;
}
}
void down(){
int fa=,son=fa<<;
while (son<=siz){
if (son+<=siz&&heap[son+]<heap[son]) son++;
if (heap[fa]<=heap[son]) break;
heap_swap(heap[fa],heap[son]);
fa=son,son<<=;
}
}
void dijkstra(Map &map,int s){
siz=n;
memset(map.dist,,sizeof(map.dist));
memset(bo,,sizeof(bo));
for (int i=;i<=n;i++) heap[i].val=inf;
for (int i=;i<=n;i++) heap[i].num=i;
for (int i=;i<=n;i++) pos[i]=i;
heap[s].val=,up(pos[s]);
for (;;){
if (heap[].val==inf||!siz) break;
int u=heap[].num;
bo[u]=true,map.dist[u]=heap[].val;
heap_swap(heap[],heap[siz--]),down();
for (int p=map.now[u],v=map.son[p];p;p=map.pre[p],v=map.son[p]){
if (heap[pos[v]].val>map.dist[u]+map.val[p]&&!bo[v])
heap[pos[v]].val=map.dist[u]+map.val[p],up(pos[v]);
}
}
}
void dfs(Map &map,int u,int t){
dfn[u]=low[u]=++idx;
for (int p=map.now[u],v=map.son[p];p;p=map.pre[p],v=map.son[p])
if (!dfn[v]){
dfs(map,v,p^),low[u]=min(low[u],low[v]);
if (low[v]>dfn[u]) road[pos[p>>]]=;
}
else if (t!=p) low[u]=min(low[u],dfn[v]);
}
int main(){
read(n),read(m),read(s),read(t);
for (int i=;i<=m;i++) read(a[i]),read(b[i]),read(c[i]),map[].put(a[i],b[i],c[i]),map[].put(b[i],a[i],c[i]);
dijkstra(map[],s),dijkstra(map[],t),min_road=map[].dist[t],map[].tot=;
for (int i=;i<=m;i++) if (map[].dist[a[i]]+map[].dist[b[i]]+c[i]==min_road)
map[].put(a[i],b[i],c[i]),map[].put(b[i],a[i],c[i]),pos[map[].tot>>]=i;//,cout<<a[i]<<' '<<b[i]<<endl;
dfs(map[],s,-);
for (int i=;i<=m;i++)
if (map[].dist[a[i]]+map[].dist[b[i]]+c[i]==min_road){
if (road[i]) puts("YES");
else if (c[i]>) puts("CAN 1");
else puts("NO");
}
else if (map[].dist[a[i]]+map[].dist[b[i]]+c[i]>min_road){
if (min_road>map[].dist[a[i]]+map[].dist[b[i]]+) printf("CAN %I64d\n",map[].dist[a[i]]+map[].dist[b[i]]+c[i]-min_road+);
else puts("NO");
}
else puts("NO");
return ;
}
codeforces567E. President and Roads的更多相关文章
- Codeforces Round #Pi (Div. 2) E. President and Roads tarjan+最短路
E. President and RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/567 ...
- 【CodeForces 567E】President and Roads(最短路)
Description Berland has n cities, the capital is located in city s, and the historic home town of th ...
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- Codeforces.567E.President and Roads(最短路 Dijkstra)
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...
- cf567E. President and Roads(最短路计数)
题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...
- CF567E President and Roads
\(\color{#0066ff}{ 题目描述 }\) 给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0) \( ...
- Codeforces Round #Pi (Div. 2) 567E President and Roads ( dfs and similar, graphs, hashing, shortest paths )
图给得很良心,一个s到t的有向图,权值至少为1,求出最短路,如果是一定经过的边,输出"YES",如果可以通过修改权值,保证一定经过这条边,输出"CAN",并且输 ...
- codeforces 567 E. President and Roads 【 最短路 桥 】
给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0) 正反向建图,分别求出起点到每个点的最短距离,终点到每个点的最 ...
- 【Henu ACM Round#14 F】 President and Roads
[链接] 我是链接,点我呀:) [题意] 在这里输入题意 [题解] 处理出起点到任意点的最短路以及最短路条数=>dis[0][i],cnt[0][i] 然后 把所有的边反向 处理出在反图上终点到 ...
随机推荐
- js 魔鬼训练
1.Object.assign 偷梁换柱 / 融合 - 将多个对象合并到第一个对象中去.这样一来methods对象中就包含着data对象了.否则this无法正常访问data中的title var ne ...
- [Flux] Component / Views
The application will dislay a some catalogs, and each catalog has title image, description. Catalog: ...
- InnoDB主要数据结构及调用流程
InnoDB主要数据结构及调用流程 InnoDB是MySQL中常用的数据引擎.本文将从源码级别对InnoDB重点数据结构和调用流程进行分析. 主要数据结构(buf0buf.h) Buf_pool Bu ...
- eclipse开发servlet应用,Tomcat无法访问jpg图片
今天遇到个奇怪的问题,我发现我放在tomcat的webapps文件夹项目下的jpg文件,浏览器无法访问: 以前没有遇到过这个问题,我知道肯定能访问的,因为以前做过相关应用,不知道问题出在哪. 后来我把 ...
- codevs 1222 信与信封问题
/* 二分图 题目给出的是确定不连通的边 如果我们拿剩下的可能联通也可能不连通的边跑最大匹配 如果不是完美非配 也就是说把所有可能的边都认为是一定的 这样都跑不出来(不能匹配到每个点)那么一定不能确定 ...
- 依赖注入与Unity(一) 介绍
在你学习依赖注入和Unity之前,你需要明白你为什么要使用它们.为了明白为什么要使用它们,你应该明白依赖注入和Unity能够帮助你解决什么类型的问题.作为介绍部分,这一章不会涉及太多关于Uni ...
- (转)xcode报Could not find a storyboard named...错误的解决办法
首先确定是否有用到storyboard 如果没有用到的话,需要将涉及到storyboard的地方修改: 1 删除plist文件里的设置 2 修改程序中使用到storyboard的地方 如果确实有使用s ...
- Java安全发布的理解
看<Java并发编程实战>遇到如下问题 代码: /** * Created by yesiming on 16/11/11. */public class Holder { private ...
- android 05
控件:RadioButton CheckedBox RatingBar ProgressBar 下拉列表:ListView Spinner <!-- 单选按钮必须放在单选按钮组当中才能生效 ,并 ...
- 跟我学android-Android应用基本组件介绍(五)
Activity activity 是最基本的模块,我们成为活动,一个activity通常就是一个单独的屏幕,每一个活动都被实现为一个独立的类,且都继承活动的基类.在activity的实现类里显示用户 ...