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: ...
随机推荐
- Jquery中拿到相同的对应的所有的标签
在Jquery中相同的ID号不能用$()获得,即使是$().each()也不能获得所有的ID相同的元素,只能获得第一个匹配的元素. 比如: 以上4个div,如果用$("#jevoly&quo ...
- day06-08初识面向对象
一.面向过程 VS 面向对象 面向过程的程序设计的核心是过程(流水线式思维),过程即解决问题的步骤,面向过程的设计就好比精心设计好一条流水线,考虑周全什么时候处理什么东西.优点是:极大的降低了写程序的 ...
- java 1.8 内存告警问题
Java HotSpot(TM) 64-Bit Server VM warning: ignoring option PermSize=512m; support was removed in 8.0 ...
- 日常记录-Pandas Cookbook
Cookbook 1.更新内容 2.关于安装 3.Pandas使用注意事项 4.包环境 5.10分钟Pandas初识 6.教程 7.Cookbook 8.数据结构简介 9.基本功能 10.使用文本数据 ...
- jquery-ui实现拖拽功能
https://www.runoob.com/jqueryui/jqueryui-tutorial.html
- Java Spring 两大特色
0 引言 本文主要描述的是Spring常用的两大特色功能:AOP和IoC容器 1 IoC Spring的IoC:就是常说的“控制反转”,也又叫依赖注入的(DI). 优点:IoC最大的好处就是把对象生成 ...
- Project Euler 23 Non-abundant sums( 整数因子和 )
题意: 完全数是指真因数之和等于自身的那些数.例如,28的真因数之和为1 + 2 + 4 + 7 + 14 = 28,因此28是一个完全数. 一个数n被称为亏数,如果它的真因数之和小于n:反之则被称为 ...
- Project Euler 15 Lattice paths
题意:在20×20方阵中从起点出发只允许向右或向下移动到达终点的路径有多少条. 思路:每次只能向右或者向下,总共 40 步,也就是 40 步中每一步都有两种选择,也就是 C (40 , 20) . 为 ...
- nyoj56-阶乘因式分解(一)
56-阶乘因式分解(一) 内存限制:64MB时间限制:3000msSpecial Judge: No accepted:6submit:7 题目描述: 给定两个数m,n,其中m是一个素数. 将n(0& ...
- Linux设备驱动--块设备(三)之程序设计(转)
http://blog.csdn.net/jianchi88/article/details/7212701 块设备驱动注册与注销 块设备驱动中的第1个工作通常是注册它们自己到内核,完成这个任务的函数 ...