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

思路:先起始两点求两遍单源最短路,利用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. [LeetCode] 155. minStack 设计最小栈

    注意:getMin()时间复杂度为O(1) 最原始的方法: class MinStack(object): def __init__(self): """ initial ...

  2. 紫书 例题8-18 UVa 1442 (扫描法)

    从左往右扫描一遍, 得从每个位置往右伸长不会碰到天花板的高度, 右往左一样, 取最小, 然后就是可以放"水"的高度了 #include<cstdio> #include ...

  3. 【codeforces 367C】Sereja and the Arrangement of Numbers

    [题目链接]:http://codeforces.com/problemset/problem/367/C [题意] 我们称一个数列a[N]美丽; 当且仅当,数列中出现的每一对数字都有相邻的. 给你n ...

  4. UVALive 6084 Happy Camper(数学题)

    题目链接:https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_ ...

  5. vijos - P1543极值问题(斐波那契数列 + 公式推导 + python)

    P1543极值问题 Accepted 标签:[显示标签] 背景 小铭的数学之旅2. 描写叙述 已知m.n为整数,且满足下列两个条件: ① m.n∈1,2.-,K ② (n^ 2-mn-m^2)^2=1 ...

  6. Mysql常见更改密码方法

    ERROR (HY000): Can't connect to local MySQL server through socket '/var/lib/mysql/mysql.sock' (2) ch ...

  7. 网络通信-ping命令

  8. java9新特性-11-String存储结构变更

    1. 官方Feature JEP254: Compact Strings 2. 产生背景 Motivation The current implementation of the String cla ...

  9. 【DNN 系列】 添加模块后不显示

    添加模块后不显示分为几个原因 1.检查.dnn文件是否填写正确,要和对应的页面文件对应上 我有一步是这这个名称地方我填上了 就不显示了.这里需要注意,VIEW 的名城是不需要写的 2.重写文件 实体操 ...

  10. 3339: Rmq Problem

    Time Limit: 20 Sec  Memory Limit: 128 MBSubmit: 1269  Solved: 665[Submit][Status][Discuss] Descripti ...