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判圈 ...
随机推荐
- ECharts使用问题
Echarts官网上给的例子,在最后有一个分号. 使用ajax请求,在eval()转化时出现错误,原因就是因为多了一个分号
- sql之函数及流程控制
date_format函数
- 小程序隐藏或自定义 scroll-view滚动条
css 隐藏滚动条 ::-webkit-scrollbar { width:; height:; color: transparent; } 自定义滚动条样式 ::-webkit-scrollbar ...
- CZGL.AliIoTClient 文档:说明
文档目录: 说明 1. 连接阿里云物联网 2. IoT 客户端 3. 订阅Topic与响应Topic 4. 设备上报属性 4.1 上报位置信息 5. 设置设备属性 6. 设备事件上报 7. 服务调用 ...
- 密码破解工具John the Ripper使用说明
John the Ripper John 包描述 John the Ripper 既功能丰富又运行快速. 它在一个程序中结合了几种破解模式,并且可以根据您的特定需求进行全面地配置(你甚至可以使用支持C ...
- 黑马MySQL数据库学习day02 表数据CRUD 约束CRUD
/* 基础查询练习: 1.字段列表查询 当查询全部字段时,一种简便方式,使用*代替全部字段(企业中不推荐使用) 2.去除重复行 DISTINCT,注意修饰的是行,也就是整个字段列表,而不是单个字段. ...
- ScrollTo:实现平滑滚动到页面指定位置
ScrollTo:实现平滑滚动到页面指定位置 ScrollTo是一款基于jQuery的滚动插件,当点击页面的链接时,可以平滑地滚动到页面指定的位置.适用在一些页面内容比较多,页面长度有好几屏的场合,本 ...
- Flask (一) 简介
Flask简介 Flask是一个基于Python实现的Web开发‘微’框架 'MicroFramework' Django是一个重型框架 官方文档: http://flask.pocoo.org/do ...
- Technocup 2017 - Elimination Round 1 (Unofficially Open for Everyone, Rated for Div. 2) B
Vasily exited from a store and now he wants to recheck the total price of all purchases in his bill. ...
- ecshop属性 {$goods.goods_attr|nl2br} 标签的赋值相关
1.nl2br() 函数在字符串中的每个新行 (\n) 之前插入 HTML 换行符 (<br />). 2. 如果要向{$goods.goods_attr|nl2br}赋新值,这个值是保存 ...