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

思路:先起始两点求两遍单源最短路,利用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. FTP 无法获取目录列表的处理方法

    FTP 无法获取目录列表的处理方法 1.以阿里云的服务器为例 对于阿里云的服务器是因为阿里云为了进一步保护用户的安全利益使用了安全策略组,我们要设置安全策略组对应的端口开启. 首先要设置端口范围,这个 ...

  2. Java基础学习总结(2)——接口

    一.接口的概念 JAVA是只支持单继承的,但现实之中存在多重继承这种现象,如"金丝猴是一种动物",金丝猴从动物这个类继承,同时"金丝猴是一种值钱的东西",金丝猴 ...

  3. 常用Java开源库(新手必看)

    Jakarta common: Commons LoggingJakarta Commons Logging (JCL)提供的是一个日志(Log)接口(interface),同时兼顾轻量级和不依赖于具 ...

  4. ASP.NET-缓存outputcache参数

    给Index加一个60秒的缓存,应该缓存在IIS服务器里面(我猜的) 只对变化的参数page不进行缓存,其他参数返回相同的内容 根据接受的语言的不同不进行缓存 设定缓存的位置 依赖于数据库变化的缓存 ...

  5. SVN过滤设置

    为了方便管理我们的系统版本号.非常多人会用到SVN,开发中我们经经常使用到SVN插件, 可是对于某些文件的缓存来说, 我们仅仅要有操作缓存便会保存一次, 每次提交非常是麻烦, 可能有的文件或者目录我们 ...

  6. SharePoint Search之(七)Search result- 结果源

    在使用搜索引擎的时候.非常多情况下,用户希望限定一下搜索范围,以便更加easy找到想要的结果. watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvU1BGYXJ ...

  7. less11 属性合并

    less //+ 合并以后,以逗号分割属性值 .mixin() { box-shadow+: inset 0 0 10px #555 ; } .myclass { .mixin(); box-shad ...

  8. Xshell高级后门完整分析报告

    Xshell高级后门完整分析报告 from:https://security.tencent.com/index.php/blog/msg/120 1. 前言 近日,Xshell官方发布公告称其软件中 ...

  9. FZOJ--2212--Super Mobile Charger(水题)

    Problem 2212 Super Mobile Charger Accept: 3    Submit: 11 Time Limit: 1000 mSec    Memory Limit : 32 ...

  10. Dictionaries

    A dictionary is like a list, but more general. In a list, the indices have to be integers; in a dict ...