给出一个有向图,从起点走到终点(必须走最短路),问一条边是否一定会被经过,如果不经过它,可以减小它的多少边权使得经过它(边权不能减少到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 【 最短路 桥 】的更多相关文章

  1. Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥

    题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...

  2. 【CodeForces 567E】President and Roads(最短路)

    Description Berland has n cities, the capital is located in city s, and the historic home town of th ...

  3. Codeforces.567E.President and Roads(最短路 Dijkstra)

    题目链接 \(Description\) 给定一张有向图,求哪些边一定在最短路上.对于不一定在最短路上的边,输出最少需要将其边权改变多少,才能使其一定在最短路上(边权必须为正,若仍不行输出NO). \ ...

  4. cf567E. President and Roads(最短路计数)

    题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...

  5. 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 ...

  6. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

  7. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...

  8. Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥

    原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...

  9. Codeforces 543B Destroying Roads(最短路)

    题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1: ...

随机推荐

  1. 文件类型总结 MIME

    来源网上https://www.cnblogs.com/zhongcj/archive/2008/11/03/1325293.html {".3gp", "video/3 ...

  2. RHEL 7 & CentOS 7禁用IPV6(转载)

    RHEL 7 & CentOS 7下禁用IPV6的方法和之前的版本不太一样了,本文整理了一下处理方法: 首先,我们必须给出最根本的解决方法:修改grub,在引导时就不加载IPV6模块 这样修改 ...

  3. appium不能获取webview内容的解决办法

    在用appium对小猿搜题app进行自动化测试时,准备用page_source打印出文章的xml内容 但是发现只能打印出外部结构内容,实际的文章内容却没有显示 截图如下 查询之后,得知需要通过cont ...

  4. [luogu 2324][SCOI 2005] 骑士精神 (A*算法)

    Description 在一个5×5的棋盘上有12个白色的骑士和12个黑色的骑士, 且有一个空位.在任何时候一个骑士都能按照骑士的走法(它可以走到和它横坐标相差为1,纵坐标相差为2或者横坐标相差为2, ...

  5. django rest-farme-work 的使用(3)

    请求和响应 Requests and Responses 从这一片来说,我们将真正开始覆盖REST框架的核心.我们来介绍一些基本的构建块 Request objects REST框架引入了一个Requ ...

  6. tp3.1 白板不报错

    今天有碰上了这种情况, 一般记忆力好把刚才改动的地方恢复一下就好了,但是今天特殊原因编辑器不小心关了,也不知道把那里改坏了,一通乱找,也找不到.汗! 没办法,提交代码几面,用git看下改变的地方,是c ...

  7. 利用Selenium实现图片文件上传的两种方式介绍

    在实现UI自动化测试过程中,有一类需求是实现图片上传,这种需求根据开发的实现方式,UI的实现方式也会不同. 一.直接利用Selenium实现 这种方式是最简单的一种实现方式,但是依赖于开发的实现. 当 ...

  8. Elasticsearch Sliced Scroll分页检索案例分享

    面试:你懂什么是分布式系统吗?Redis分布式锁都不会?>>>   The best elasticsearch highlevel java rest api-----bboss ...

  9. Arduino基本函数介绍

    转载自http://cnlearn.linksprite.com/?p=5248#.VwZrzvl95hE 数字 I/O (1)pinMode(pin, mode) 数字IO 口输入输出模式定义函数, ...

  10. HDU 4767

    昨晚苦恼了一晚,因为即将大三了,必须要准备实习什么的事了.一般都会去公司实习吧,但是看看自己的简历,实在拿不出手,因为大一大二都在搞ACM(虽然真正搞的只有大二一年),但却没有什么成绩,又不愿意做项目 ...