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

思路:先起始两点求两遍单源最短路,利用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. 脚本2,从1到99 ,添加用户user1,user2,。。。 user99

    脚本2,从1到99 ,添加用户user1,user2,... user99 如果用户user$存在,脚本显示用户存在,否则添加用户,并显示添加用户成功. 脚本如下 [root@localhost ho ...

  2. ElementUi rules表单验证

    ElementUi 表单验证 工作中常用到的JS验证 可以在pattern中书写正则,并且配合elementUI进行表单验证. pattern 属性规定用于验证输入字段的模式.模式指的是正则表达式. ...

  3. django xadmin插件 的基本用法 1

    1  安装或导入 xadmin 1 pip 安装 2 源码导入 在新建项目中新建extra_apps文件夹并将下载后的源码解压放入 (推荐,方便后续我们可以在源码中自定义一些插件的使用) 注: 具体可 ...

  4. 阿里云CentOS系统配置iptables防火墙

    虽说阿里云推出了云盾服务,但是自己再加一层防火墙总归是更安全些,下面是我在阿里云vps上配置防火墙的过程,目前只配置INPUT.OUTPUT和FORWORD都是ACCEPT的规则 一.检查iptabl ...

  5. Android与设计模式——单例(Singleton)模式

    概念: java中单例模式是一种常见的设计模式.单例模式分三种:懒汉式单例.饿汉式单例.登记式单例三种. 单例模式有一下特点: 1.单例类仅仅能有一个实例. 2.单例类必须自己自己创建自己的唯一实例. ...

  6. mysql-计算字段

    一.计算字段 存储在数据库表中的数据一般不是应用程序所需要的格式 1.如果想在一个字段中既显示公司名,又显示公司的地址,但这两个信息一般包含在不同的字段中. 2.城市.州和邮编存储在不同的列中,但邮件 ...

  7. 一入python深似海--python之道

    python社区不乏幽默.先来看"python之道"这首诗. 导入this包: import this 输出是一首诗,这首诗总结了Python的风格,能够指导Python程序猿的编 ...

  8. jquery开发之代码风格

    1,链式操作风格. (1) 对于同一个对象不超过三个操作的.可直接写成一行.代码例如以下: $("li").show().unbind("click"); (2 ...

  9. 2.mongoDB 介绍(特点、优点、原理)

    转自:https://www.cnblogs.com/hoojo/archive/2011/06/01/2066119.html 介绍:MongoDB是一个基于分布式文件存储的数据库.由C++语言编写 ...

  10. Tomcat vs. Jetty vs. Undertow: Comparison of Spring Boot Embedded Servlet Containers

    原文地址:https://examples.javacodegeeks.com/enterprise-java/spring/tomcat-vs-jetty-vs-undertow-compariso ...