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: ...
随机推荐
- 文字横向滚动marquee
<div style="width:200px; height:300px"> <marquee behavior="scroll" cont ...
- nginx视频服务缓存方案设置指导
本文描述了如何通过设置nginx缓存达到降低服务器后端压力的效果以及结合nginx第三方插件ngx_cache_purge实现nginx缓存后的自动清理功能.具体实施步骤如下所示:第一步:获取清除清除 ...
- 取得Linux系统的各种统计信息
本文基于Linux 2.6.x内核 一.取得CPU信息(相关文件/proc/stat) 在一个系统中的/proct/stat文件内容如下 $ cat /proc/stat cpu 1039426 17 ...
- HDU 2522 A simple problem( 分数循环节 )
链接:Here! 思路:模拟除法,当余数再次出现的时候一定是遇到了循环节( 可看下图例子 ),否则的话继续除法的步骤,直到被除数为 0 . 注意:这道题不需要重新申请一个数组来单独存放答案,如果符合要 ...
- UVA1585 - Score(紫书习题3.1)
如何计算你们的得分呢?,如"OOXXOXXOOO". "O"表示问题的正确答案,"X"表示错误的答案.那么它得分是由它自己和它刚刚以前连续的 ...
- php 返回某个月的 每周几有几天
不得不承认,这真是一个奇葩的需求,无奈写个类凑活用用. 输入日期格式或者 时间戳,返回当月有多少个周一.周二.周三.....周日; 思路就是 找到这个月有多少天,在便利判断. 稍微考虑下闰年的情况 前 ...
- 关于springboot整合的详细过程
Spring-boot http://tengj.top/2017/04/24/springboot0/
- 设置编码格式为utf8
response.setCharacterEncoding("UTF-8"); 在Servlet2.3中是不行的,至少要2.4版本才可以,如果低于2.4版本,可以用如下办法: re ...
- [ASP.NET]asp.net动态加载用户控件
用户控件 // 用户控件源码 namespace wzjr.control { public partial class Topic : System.Web.UI.UserControl { pub ...
- Ruby中使用patch HTTP方法
Ruby中使用patch HTTP方法 如果使用patch,在后台可以看到只更新了改动的部分: Started PATCH "/ads/5/update" for ::1 at 2 ...