codeforces Gym 100338C Important Roads (重建最短路图)
正反两次最短路用于判断边是不是最短路上的边,把最短路径上的边取出来建图。然后求割边。注意重边,和卡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 (重建最短路图)的更多相关文章
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- Gym - 100338C Important Roads 最短路+tarjan
题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化. 思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图.在新图中找到 ...
- Codeforces Gym 100338C C - Important Roads tarjan
C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...
- Codeforces Gym 100269 Dwarf Tower (最短路)
题目连接: http://codeforces.com/gym/100269/attachments Description Little Vasya is playing a new game na ...
- codeforces 544 D Destroying Roads 【最短路】
题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs ...
- Codeforces GYM 100876 J - Buying roads 题解
Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...
- Codeforces 806 D.Prishable Roads
Codeforces 806 D.Prishable Roads 题目大意:给出一张完全图,你需要选取其中的一些有向边,连成一个树形图,树形图中每个点的贡献是其到根节点路径上每一条边的边权最小值,现在 ...
- Codeforces 191C Fools and Roads(树链拆分)
题目链接:Codeforces 191C Fools and Roads 题目大意:给定一个N节点的数.然后有M次操作,每次从u移动到v.问说每条边被移动过的次数. 解题思路:树链剖分维护边,用一个数 ...
- Codeforces Gym 101252D&&floyd判圈算法学习笔记
一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...
随机推荐
- c/c++面试39-44之内存动态分配
39 指针的初始化(二叉树排序),其中引入了双向链表 #include <stdio.h> #include <stdlib.h> struct Tag_Node { stru ...
- springMVC拦截配置
1.web.xml文件配置 <!-- spring mvc --> <servlet> <servlet-name>DispatcherServlet</se ...
- setInterval和setTImeout中的this指向问题
问题:在setInterval和setTimeout中传入函数时,函数中的this会指向window对象 解决方法: 1. 将当前对象的this存为一个变量,定时器内的函数利用闭包来访问这个变量.va ...
- [Xcode 实际操作]九、实用进阶-(30)为IAP(支付方式)内购项目添加测试账号,测试内购功能
目录:[Swift]Xcode实际操作 本文将演示如何添加测试账号,以方便对内购功能进行测试. IAP,即in-App Purchase ,是一种智能移动终端应用程序付费的模式, 在苹果(Apple) ...
- IT兄弟连 JavaWeb教程 MVC设计模式
MVC是Model-View-Controller的简称,即模型-视图-控制器.MVC是一种设计模式,它强制性地把应用程序的数据展示.数据处理和流程控制分开.MVC把应用程序分成3个核心模块:模型.视 ...
- 【OpenJ_Bailian - 2287】Tian Ji -- The Horse Racing (贪心)
Tian Ji -- The Horse Racing 田忌赛马,还是English,要不是看题目,我都被原题整懵了,直接上Chinese吧 Descriptions: 田忌和齐王赛马,他们各有n匹马 ...
- maven - settings.xml文件详解
Settings.xml配置文件详解 maven默认的settings.xml是一个包含注释和例子的模板,可以快速的修改settings.xml文件 maven安装后不会在用户目录下自动生成setti ...
- ios wkwebview 跳转到新的controllerview加载页面 出现闪退问题
func webView(_ webView: WKWebView, didStartProvisionalNavigation navigation: WKNavigation!) { if(isF ...
- SpringBoot | SpringBoot启动错误
Error starting ApplicationContext. To display the conditions report re-run your application with 'de ...
- tagName
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...