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判圈 ...
随机推荐
- tcp/ip详解(转)
与UDP不同的是,TCP提供了一种面向连接的.可靠的字节流服务.TCP协议的可靠性主要有以下几点保障: (1)应用数据分割成TCP认为最适合发送的数据块.这部分是通过“MSS”(最大数据包长度)选项来 ...
- ubuntu上安装与卸载deb文件(转载)
转自:http://blog.csdn.net/nkguohao/article/details/8951082 版权声明:本文为博主原创文章,未经博主允许不得转载. 通过deb包安装软件: sudo ...
- 【WIP_S9】图论算法
创建: 2018/06/01 图的概念 有向边 有向图 无向边 无向图 点的次数: 点连接的边的数量 闭路: 起点和重点一样 连接图: 任意两点之间都可到达 无闭路有向图: 没有闭路的有向图 森林: ...
- 豆瓣api获取图片403
1.问题描述 豆瓣的图片资源在网页中不能正常显示,403禁止访问,把地址放到浏览器中就可以正常访问了? 原因是豆瓣现在有防盗链 2.解决问题 在页面中加上 <meta name="re ...
- 洛谷P2485 [SDOI2011]计算器(exgcd+BSGS)
传送门 一题更比三题强 1操作直接裸的快速幂 2操作用exgcd求出最小正整数解 3操作用BSGS硬上 然后没有然后了 //minamoto #include<cstdio> #inclu ...
- 详解Codis安装与部署
Codis github上的介绍安装,里面很全,而且也有中/英文的,只不过按照github的步骤安装,会有一些坑,所以有了这么一篇文章. 在上一篇文章<Redis实用监控工具一览>中,介绍 ...
- Sublime Text 报“Pylinter could not automatically determined the path to lint.py
Pylinter could not automatically determined the path to lint.py. please provide one in the settings ...
- python模块之struct
# #********struct模块********# # 1.按照指定格式将Python数据转换为字符串,该字符串为字节流,如网络传输时, # 不能传输int,此时先将int转化为字节流,然后再发 ...
- [软件工程基础]2017.11.04 第八次 Scrum 会议
具体事项 项目交接燃尽图 每人工作内容 成员 已完成的工作 计划完成的工作 工作中遇到的困难 游心 #10 搭建可用的开发测试环境:#9 阅读分析 PhyLab 后端代码与文档:#8 掌握 Larav ...
- 执行脚本 提示 command not found
问题现象: 初学shell,写了个脚本, 1.从windows 写好 脚本,然后部署到 linux 上. 2.chmod +x之后执行提示command not found,系统环境redhat9,用 ...