题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化。

思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图。在新图中找到所有的桥输出就可以了。

 #include <iostream>
#include <cstdio>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#define LL long long
#define eps 1e-8
#define INF 0x3f3f3f3f
#define MAXN 20005
#define MAXM 100005
using namespace std; struct Edge
{
int from, to, dist, pos;
Edge(int from, int to, int dist, int pos) :from(from), to(to), dist(dist), pos(pos){};
};
struct HeapNode
{
int d, u;
HeapNode(int d, int u) :d(d), u(u){};
bool operator <(const HeapNode& rhs) const{
return d > rhs.d;
}
};
struct Dijstra
{
int n, m;
vector<Edge> edges;
vector<int> G[MAXN];
bool done[MAXN];
int d[MAXN];
int p[MAXN]; void init(int n){
this->n = n;
for (int i = ; i <= n; i++){
G[i].clear();
}
edges.clear();
} void AddEdge(int from, int to, int dist, int pos = ){
edges.push_back(Edge(from, to, dist, pos));
m = edges.size();
G[from].push_back(m - );
} void dijstra(int s){
priority_queue<HeapNode> Q;
for (int i = ; i <= n; i++){
d[i] = INF;
}
d[s] = ;
memset(done, , sizeof(done));
Q.push(HeapNode(, s));
while (!Q.empty()){
HeapNode x = Q.top();
Q.pop();
int u = x.u;
if (done[u]) continue;
done[u] = true;
for (int i = ; i < G[u].size(); i++){
Edge& e = edges[G[u][i]];
if (d[e.to] > d[u] + e.dist){
d[e.to] = d[u] + e.dist;
p[e.to] = G[u][i];
Q.push(HeapNode(d[e.to], e.to));
}
else if (d[e.to] == d[u] + e.dist){ }
}
}
}
};
int pre[MAXN], isbridge[MAXM], low[MAXN];
vector<Edge> G[MAXN];
int dfs_clock;
int dfs(int u, int father){
int lowu = pre[u] = ++dfs_clock;
//int child = 0;
for (int i = ; i < G[u].size(); i++){
int v = G[u][i].to;
if (!pre[v]){
//child++;
int lowv = dfs(v, G[u][i].pos);
lowu = min(lowu, lowv);
if (lowv > pre[u]){
isbridge[G[u][i].pos] = true;
}
}
else if (pre[v] < pre[u] && G[u][i].pos != father){
lowu = min(lowu, pre[v]);
}
}
low[u] = lowu;
return lowu;
}
Dijstra s, t;
vector<Edge> edges; int res[MAXM];
int main()
{
#ifdef ONLINE_JUDGE
freopen("important.in", "r", stdin);
freopen("important.out", "w", stdout);
#endif // OPEN_FILE
int n, m;
while (~scanf("%d%d", &n, &m)){
s.init(n);
t.init(n);
edges.clear();
int x, y, z;
for (int i = ; i <= m; i++){
scanf("%d%d%d", &x, &y, &z);
edges.push_back(Edge(x, y, z, i));
edges.push_back(Edge(y, x, z, i));
s.AddEdge(x, y, z);
s.AddEdge(y, x, z);
t.AddEdge(x, y, z);
t.AddEdge(y, x, z);
}
s.dijstra();
t.dijstra(n);
LL dis = s.d[n];
//把所有最短路径找出来,在里面找出所有的桥就是答案
for (int i = ; i < edges.size(); i++){
Edge e = edges[i];
if (s.d[e.from] + e.dist + t.d[e.to] == dis){
G[e.from].push_back(Edge(e.from, e.to, e.dist, e.pos));
G[e.to].push_back(Edge(e.to, e.from, e.dist, e.pos)); }
}
dfs_clock = ;
memset(isbridge, , sizeof(isbridge));
memset(pre, , sizeof(pre));
dfs(, -);
int ans = ;
for (int i = ; i <= m; i++){
if (isbridge[i]){
ans++;
res[ans] = i;
}
}
printf("%d\n", ans);
for (int i = ; i <= ans; i++){
printf("%d ", res[i]);
}
printf("\n");
}
}

Gym - 100338C Important Roads 最短路+tarjan的更多相关文章

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

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

  2. codeforces Gym 100338C Important Roads (重建最短路图)

    正反两次最短路用于判断边是不是最短路上的边,把最短路径上的边取出来建图.然后求割边.注意重边,和卡spfa. 正权,好好的dijkstra不用,用什么spfa? #include<bits/st ...

  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. ACdream 1415 Important Roads

    Important Roads Special JudgeTime Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Ja ...

  5. CF Destroying Roads (最短路)

    Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard in ...

  6. Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接: 题目 D. Destroying Roads time limit per test 2 seconds memory limit per test 256 megabytes input ...

  7. Codeforces Round #Pi (Div. 2) E. President and Roads 最短路+桥

    题目链接: http://codeforces.com/contest/567/problem/E 题意: 给你一个带重边的图,求三类边: 在最短路构成的DAG图中,哪些边是必须经过的: 其他的(包括 ...

  8. cf567E. President and Roads(最短路计数)

    题意 题目链接 给出一张有向图,以及起点终点,判断每条边的状态: 是否一定在最短路上,是的话输出'YES' 如果不在最短路上,最少减去多少权值会使其在最短路上,如果减去后的权值\(< 1\),输 ...

  9. 【poj 1724】 ROADS 最短路(dijkstra+优先队列)

    ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N ...

随机推荐

  1. H3C S5130交换机堆叠操作

    配置过程中注意事项: 1.最好提前定义好IRF的主从设备,可通过IRF优先级进行定义,越大越优 2.一定要在使能IRF之前就保存配置(因为使能过程中,会出现设备重启的情况,如果设备重启后配置丢失,会导 ...

  2. java+jsp+sqlserver实现简单的增删改查操作 连接数据库代码

    1,网站系统开发需要掌握的技术 (1)网页设计语言,html语言css语言等 (2)Java语言 (3)数据库 (4)等 2,源程序代码 (1) 连接数据库代码 package com.jaovo.m ...

  3. nodejs 守护进程运行

    有四种方法: 1.forever forver start  bin/www 2.pm2 pm2 strat bin/www 3.node自身进程保护 nohup node /bin/www  > ...

  4. 时间就是金钱HNCOI2000(最短路)

    时间就是金钱HNCOI2000 版权声明:本篇随笔版权归作者YJSheep(www.cnblogs.com/yangyaojia)所有,转载请保留原地址! 人们总是选时间最短或费用最低的路线 例如, ...

  5. VT-x is disabled in the BIOS. (VERR_VMX_MSR_VMXON_DISABLED)

    参考以下文章: http://94it.net/a/jingxuanboke/2014/0717/368367.html

  6. 第8章2节《MonkeyRunner源代码剖析》MonkeyRunner启动执行过程-解析处理命令行參数

    MonkeyRunnerStarter是MonkeyRunner启动时的入口类,由于它里面包括了main方法.它的整个启动过程主要做了以下几件事情: 解析用户启动MonkeyRunner时从命令行传输 ...

  7. Linux 经常使用快捷键

    桌面下: Alt+F5   取消最大化窗体 Alt+F9   最小化窗体  Alt+F10  最大化窗体  Alt+空格 打开窗体的控制菜单 (点击窗体左上角图标出现的菜单)     ctl+r   ...

  8. 几种常见sqlalchemy查询:

        #简单查询     print(session.query(User).all())     print(session.query(User.name, User.fullname).all ...

  9. poj_3667线段树区间合并

    对照着notonlysuccess大牛的代码写的 #include<iostream> #include<cstdio> #include<cstring> #in ...

  10. ckeidtor编辑器添加图片上传功能

    1.ckeditor默认没有上传图片功能,只能通过Url显示图片,图下图 2.首先说明,ckeditor是有上传功能的,只是隐藏了,需要通过配置让它显示 找到ckeditor/plugins/imag ...