codeforces 567 E. President and Roads 【 最短路 桥 】
给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到0)
正反向建图,分别求出起点到每个点的最短距离,终点到每个点的最短距离(用这个可以算出减小的边权)
再将在最短路径上的边重新建图。求出里面的桥,就是必须经过的边
wa了一上午------呜呜呜呜
先wa 19 是因为求桥的时候是无向图,数组开小了一半
然后 wa 46 ,是因为dis[]数组初始化为 1 << 30 -1 ,应该再开大点 ,开成 1 << 50 -1
我写成 1 LL * 50 -----一直wa 19------
5555555555555555555--------sad-------
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <cmath>
#include <vector>
#include <map>
#include <set>
#include <stack>
#include <queue>
#include <iostream>
#include <algorithm>
using namespace std;
#define lp (p << 1)
#define rp (p << 1|1)
#define getmid(l,r) (l + (r - l) / 2)
#define MP(a,b) make_pair(a,b)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int,int> pii;
const int INF = ( << ) - ;
const int maxn = ; int N,M;
ll path;
int first1[maxn],nxt1[ * maxn],ecnt1;
int first2[maxn],nxt2[ *maxn],ecnt2;
int first[maxn],nxt[*maxn],ecnt;
ll dis1[maxn],dis2[maxn]; int bg[*maxn],vis[*maxn],low[maxn],dfn[maxn];
int ans[*maxn],res[maxn];
int tot,num; struct edge{
int v,u,cost;
int tag;
int ge;
int id;
friend bool operator < (edge a,edge b){
return a.cost > b.cost;
}
}; edge e1[*maxn],e2[*maxn],e[*maxn]; void init(){
ecnt1 = ecnt2 = ecnt = ;
memset(first1,-,sizeof(first1));
memset(first2,-,sizeof(first2));
memset(first,-,sizeof(first));
} void Add_edge1(int u,int v,int c){
nxt1[++ecnt1] = first1[u];
e1[ecnt1].u = u;
e1[ecnt1].v = v;
e1[ecnt1].cost = c;
e1[ecnt1].tag = ;
e1[ecnt1].ge = ;
first1[u] = ecnt1;
} void Add_edge2(int u,int v,int c){
nxt2[++ecnt2] = first2[u];
e2[ecnt2].v = v;
e2[ecnt2].cost = c;
e2[ecnt2].tag = ;
e2[ecnt2].ge = ;
first2[u] = ecnt2;
} void Add_edge(int u,int v,int c){
nxt[ecnt] = first[u];
e[ecnt].v = v;
e[ecnt].u = u;
e[ecnt].id = c;
first[u] = ecnt++; nxt[ecnt] = first[v];
e[ecnt].v = u;
e[ecnt].u = v;
e[ecnt].id = c;
first[v] = ecnt++;
} struct cmp{
bool operator ()(pii a,pii b){
return a.first > b.first;
}
}; void Dijstra1(int s){
priority_queue<pii,vector<pii >,cmp> PQ;
dis1[s] = ;
PQ.push(MP(dis1[s],s));
int cnt = ;
while(!PQ.empty()){
pii x = PQ.top(); PQ.pop();
if(dis1[x.second] < x.first) continue;
for(int i = first1[x.second]; i != -; i = nxt1[i]){
int v = e1[i].v;
if(dis1[v] > dis1[x.second] + e1[i].cost){
dis1[v] = dis1[x.second] + e1[i].cost;
PQ.push(MP(dis1[v],v));
}
}
}
} void Dijstra2(int s){
priority_queue<pii,vector<pii >,cmp> PQ;
dis2[s] = ;
PQ.push(MP(dis2[s],s));
int cnt = ;
while(!PQ.empty()){
pii x = PQ.top(); PQ.pop();
if(dis2[x.second] < x.first) continue;
for(int i = first2[x.second]; i != -; i = nxt2[i]){
int v = e2[i].v;
if(dis2[v] > dis2[x.second] + e2[i].cost){
dis2[v] = dis2[x.second] + e2[i].cost;
PQ.push(MP(dis2[v],v));
}
}
}
} void Dfs(int p,int pre){
dfn[p] = low[p] = ++tot;
for(int i = first[p]; ~i; i = nxt[i]){
int v = e[i].v;
if(vis[i]) continue;
vis[i] = vis[i ^ ] = true;
if(!dfn[v]){
Dfs(v,p);
low[p] = min(low[p],low[v]);
if(low[v] > dfn[p]) {
bg[i] = bg[i ^ ] = true;
ans[e[i].id] = ;
}
}
else low[p] = min(low[p],dfn[v]);
}
} void Tarjan(){
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
memset(bg,false,sizeof(bg));
memset(vis,false,sizeof(vis));
tot = ;
for(int i = ; i <= N; ++i) if( dfn[i] == ) Dfs(i,-);
} void solve(){
memset(res,-,sizeof(res));
for(int i = ;i <= M;i++){
int u = e1[i].u;
int v = e1[i].v;
if(ans[i] == ) {
res[i] = ;
continue;
}
ll need = path - dis1[u] - dis2[v];
if(need > ){
res[i] = e1[i].cost - need + ;
}
} for(int i = ;i <= M;i++){
if(res[i] == ) puts("YES");
else if(res[i] == -) puts("NO");
else printf("CAN %d\n",res[i]);
}
} int main(){
int a,b,c,s,t;
num = ;
while(scanf("%d%d%d%d",&N,&M,&s,&t) != EOF){
num++;
init();
int x; for(int i = ; i <= M; ++i){
scanf("%d%d%d",&a,&b,&c);
Add_edge1(a,b,c);
Add_edge2(b,a,c);
} for(int i = ;i <= N;i++) dis1[i] = dis2[i] = 1ll << ; Dijstra1(s);
Dijstra2(t);
path = dis1[t]; for(int u = ;u <= N;u++){
for(int i = first1[u];~i;i = nxt1[i]){
int v = e1[i].v;
if(dis1[u] + dis2[v] + e1[i].cost == path) {
e1[i].tag = ;
e2[i].tag = ;
Add_edge(u,v,i);
}
}
} memset(ans,,sizeof(ans));
Tarjan();
solve();
}
return ;
}
codeforces 567 E. President and Roads 【 最短路 桥 】的更多相关文章
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥
题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...
- 【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.567E.President and Roads(最短路 Dijkstra)
题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...
- cf567E. President and Roads(最短路计数)
题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...
- 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 GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路
题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Codeforces 543B Destroying Roads(最短路)
题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...
随机推荐
- nutz 自定义sql的使用
虽然提供了Cnd,但是用起来是觉得有点不方便,然后就直接编写Sql语句.nutz提供了一些方法. Nutz.Dao 的自定义 SQL 部分的解决方案是: // 不推荐使用 用户可以硬编码 SQL 语句 ...
- swift可选值总结
1.枚举结构: 2.装包.解包概念:关联值. 3.可选值声明: 4.解包: 5.可选值作为参量生成的链. 6.可选值调用链. 最后做个总结 访问可选对象的属性或方法时,可以用 ? 号 访问可选对象的属 ...
- jdbc转账操作
public class cs{ public static void main(String[] args){ try{ Connection conn=JdbcUtils.getConnectio ...
- .apply .call方法的区别及使用 .apply第二个参数为数组,.call第二个参数为参数列表, 相同点:第一个参数都为Function函数内部的this对象.
Function.apply(obj,args)方法能接收两个参数 obj:这个对象将代替Function类里this对象 args:这个是数组,它将作为参数传给Function(args--> ...
- TensorFlow+实战Google深度学习框架学习笔记(11)-----Mnist识别【采用滑动平均,双层神经网络】
模型:双层神经网络 [一层隐藏层.一层输出层]隐藏层输出用relu函数,输出层输出用softmax函数 过程: 设置参数 滑动平均的辅助函数 训练函数 x,y的占位,w1,b1,w2,b2的初始化 前 ...
- vue v-for下图片src显示失败,404错误
- 交互式编程之Golang基本配置(Jupyter-notebooks Golang)
JupyterNoteBook-GO 启动错误 Install Go Install gophernotes 参考资料 如有错误,欢迎指出 错误 error: Cannot assign reques ...
- 2019-04-03 Anaconda+VSCode搭建python开发环境,并连接GIthub
1.最好的Python开发环境 :Anaconda+VSCode搭建python开发环境,conda提供了python开发环境和大量的你不用安装的库 conda的环境变量: 直接在conda 中下载启 ...
- Django用户认证(四)自定义认证Customizing authentication
原文:https://www.cnblogs.com/linxiyue/p/4061044.html 扩展已有的用户模型Extending the existing User model 有两种方法来 ...
- centos安装wget 及配置
yum -y install wget #yum -y install setup 本文 #yum install perl Searching for GCC... The path "& ...