正反两次最短路用于判断边是不是最短路上的边,把最短路径上的边取出来建图。然后求割边。注意重边,和卡spfa。

正权,好好的dijkstra不用,用什么spfa?

#include<bits/stdc++.h>
using namespace std; typedef long long ll;
#define fi first
#define se second
#define bug(x) cout<<#x<<'='<<x<<endl;
#define FOR(i,s,e) for(int i = s; i < e; i++) const int maxn = 2e4+;
const int maxe = 2e5+;
ll d1[maxn],d2[maxn];
struct Graph
{
int head[maxn],fro[maxe],to[maxe],nxt[maxe],ecnt;
ll w[maxe];
void init() { memset(head,-,sizeof(head)); ecnt = ; }
Graph(){ init(); }
void addEdge(int u,int v,ll l)
{
fro[ecnt] = u;
to[ecnt] = v;
w[ecnt] = l;
nxt[ecnt] = head[u];
head[u] = ecnt++;
}
}G1,G3;
int id[maxe]; const ll INF = 0x3f3f3f3f3f3f3f3fLL; int cut[maxe]; // 0 not shortest path 1 shortest path 2 unique shortest path
int dfs_clock,dfn[maxn],low[maxn];
void Tarjan(int u,int fa)
{
dfn[u] = low[u] = ++dfs_clock;
for(int i = G3.head[u]; ~i; i = G3.nxt[i]){
if((i^) == fa) continue;
int v = G3.to[i];
if(!dfn[v]){
Tarjan(v,i);
low[u] = min(low[u],low[v]);
if(low[v]>dfn[u]) {
cut[id[i]] = ;
}
}else { low[u] = min(low[u],low[v]); }
}
}
struct Node
{
ll d;
int u;
bool operator < (const Node& x) const{
return d > x.d;
}
}; bool vis[maxn];
void Dijkstra(int s,Graph &g,ll (&d)[maxn])
{
memset(vis,,sizeof(vis));
priority_queue<Node> q;
memset(d,0x3f,sizeof(d));
d[s] = ;
q.push(Node{,s});
while(q.size()){
Node x = q.top(); q.pop();
int u = x.u;
if(vis[u]) continue;
vis[u] = true;
for(int i = g.head[u]; ~i; i = g.nxt[i] ){
int v = g.to[i];
if(v == u) continue;
if(d[v]> d[u]+g.w[i]){
d[v] = d[u]+g.w[i];
q.push(Node{d[v],v});
}
}
}
} vector<int> ans;
bool evis[maxe]; int main()
{
freopen("important.in","r",stdin);
freopen("important.out","w",stdout);
int n,m,s,t;
scanf("%d%d",&n,&m);
s = ; t = n;
for(int i = ; i < m; i++){
int u,v,l; scanf("%d%d%d",&u,&v,&l);
G1.addEdge(u,v,l); G1.addEdge(v,u,l);
}
Dijkstra(s,G1,d1);
Dijkstra(t,G1,d2);
ll shortest = d1[t];
for(int i = ,M = m*; i < M; i++){
int u = G1.fro[i], v = G1.to[i];
if(shortest == G1.w[i] + d1[u] + d2[v] ){
int eid = i/;
if(!evis[eid]){
evis[eid] = true;
id[G3.ecnt] = eid; G3.addEdge(u,v,G1.w[i]);
id[G3.ecnt] = eid; G3.addEdge(v,u,G1.w[i]);
}
}
}
Tarjan(s,-);
if(~G3.nxt[G3.head[s]]){
for(int i = G3.head[s]; ~i; i = G3.nxt[i]){
cut[id[i]] = ;
}
}
for(int i = ; i < m; i ++){
if(cut[i]) ans.push_back(i+);
}
int sz = ans.size();
printf("%d\n",sz--);
for(int i = ; i <= sz; i++){
printf("%d%c",ans[i],i == sz?'\n':' ');
}
return ;
}

codeforces Gym 100338C Important Roads (重建最短路图)的更多相关文章

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

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

  2. Gym - 100338C Important Roads 最短路+tarjan

    题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化. 思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图.在新图中找到 ...

  3. Codeforces Gym 100338C C - Important Roads tarjan

    C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  4. Codeforces Gym 100269 Dwarf Tower (最短路)

    题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...

  5. codeforces 544 D Destroying Roads 【最短路】

    题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...

  6. Codeforces GYM 100876 J - Buying roads 题解

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

  7. Codeforces 806 D.Prishable Roads

    Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...

  8. Codeforces 191C Fools and Roads(树链拆分)

    题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...

  9. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

随机推荐

  1. Asset Catalog Help (四)---Adding an iOS App Icon Set or Launch Image Set

    Adding an iOS App Icon Set or Launch Image Set Organize different resolutions of your app icons and ...

  2. 技术胖Flutter第四季-23静态资源和项目图片的处理

    技术胖Flutter第四季-23静态资源和项目图片的处理 视频地址:https://www.bilibili.com/video/av35800108/?p=24 项目中引用图片静态资源文件 这里就是 ...

  3. python 三元表达式 列表推导式,生成器表达式。递归,匿名函数, 内置函数

    三元表达式 三元表达式仅应用于: 1.条件成立返回一个值 2.条件不成立返回一个值 res = x if x>y else y print(res) name= input("姓名&g ...

  4. HDU 3729【二分匹配】

    题意: 给出n个同学的排名,代表每个排名在哪个区间,要求保证最多人说的是实话,并在此前提下求一个说真话人最大字典序. 思路: 最后感觉就是点去填区间,点和区间建个边,然后跑个二分图,然后sort一发. ...

  5. Unity亚洲开发者大会会议简录之技术篇,Profiler的一些参数的意思

    2014Unity亚洲开发者大会会议简录之技术篇 拖公司的福,有幸去了一趟北京参加了一场Unity3D的交流盛宴,在为期两天的时间内,不仅有着技术上收获,也有心灵上的震撼.现在先来说说技术方面的一些比 ...

  6. IT兄弟连 JavaWeb教程 Servlet会话跟踪 经典案例

    案例需求:编写一个servlet,可以向session中存放一个消息,再编写一个servlet可以从session取得session中存放的这个消息. 案例实现: package com.xdl.se ...

  7. unicode码表和标准下载 unicode官网

  8. js截取文件名不带后缀

    利用正则表达式是匹配后缀名 一般文件后缀都为.xxx,也就是说从尾部匹配的话首先是字母,接着应该有一个. 那么我们就开始写正则表达式 利用在线工具 方便我们编写边测试 一般都是小写字母,所以我们用[a ...

  9. Python -3-列表和元组

    1.用list就可以像修改列表那样修改字符串了 >>> list('Hello') ['H', 'e', 'l', 'l', 'o'] 可将任何序列作为list的参数   2.列表的 ...

  10. redis配置配置文件

    # redis 配置文件示例 # 当你需要为某个配置项指定内存大小的时候,必须要带上单位, # 通常的格式就是 1k 5gb 4m 等酱紫: # # 1k => 1000 bytes # 1kb ...