正反两次最短路用于判断边是不是最短路上的边,把最短路径上的边取出来建图。然后求割边。注意重边,和卡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. error:对‘vtable for new_sequence’未定义的引用 对‘typeinfo for num_sequence’未定义的引用

    在设计父类子类继承关系中,经常会出现此类问题. 报错原因:父类中的虚函数只有声明,没有定义. 解决方案 : 1. 定义相关的虚函数的实现. 2. 不实现了,直接搞成纯虚函数留给后代实现. virtua ...

  2. 201621123016 《Java程序设计》第九周学习总结

    1. 本周学习总结 1.1 以你喜欢的方式(思维导图或其他)归纳总结集合与泛型相关内容. 2. 书面作业 本次作业题集集合 1. List中指定元素的删除(题集题目) 1.1 实验总结.并回答:列举至 ...

  3. html页面选择图片上传时实现图片预览功能

    实现效果如下图所示 只需要将下面的html部分的代码放入你的代码即可 (注意引入jQuery文件和html头部的css样式,使用的是ajax提交) <!-- 需引入jQuery 引入样式文件 引 ...

  4. HDU 3501【欧拉函数拓展】

    欧拉函数 欧拉函数是指:对于一个正整数n,小于n且和n互质的正整数(包括1)的个数,记作φ(n) . 通式:φ(x)=x*(1-1/p1)(1-1/p2)(1-1/p3)*(1-1/p4)-..(1- ...

  5. 51Nod 1099 任务执行顺序 (贪心)

    #include <iostream> #include <algorithm> using namespace std; +; struct node{ int r, q; ...

  6. cobian backup 11 使用

    主机和备份机器创建备份用户(bf) 在备份机器上 设置备份用户, 备份目录右键安全,添加刚刚创建的备份bf用户,并授予所有权限 设置文件夹共享,并设置共享用户为刚刚创建的bf用户 并且在高级共享设置去 ...

  7. swift SqliteDB使用

    操作步骤: 1,在 Build Phases -> Link Binary With Libraries 中点击加号,添加 libsqlite3.0.tbd 到项目中来   2,创建连接头文件B ...

  8. 在centos7下搭建nginx环境,并配置负载均衡,最终能达到通过域名直接访问的目的

    1.关于nginx:个人理解的nginx它的主要用途就是负载均衡,当然可能还有其他一些功能可能我们不长用到,我们通过nginx可以干什么呢?为什么要引入它呢?原因是当有高并发访问服务器时,服务器可能会 ...

  9. socket连接 代码

    dispatch_sync(dispatch_get_global_queue(, ), ^{ // 处理耗时操作的代码块... // 创建socket /* 1.AF_INET: ipv4 执行ip ...

  10. Codeforces Round #397 by Kaspersky Lab and Barcelona Bootcamp (Div. 1 + Div. 2 combined) B

    Description Kostya likes Codeforces contests very much. However, he is very disappointed that his so ...