C - Important Roads
Time Limit: 20 Sec

Memory Limit: 256 MB

题目连接

http://acm.hust.edu.cn/vjudge/contest/view.action?cid=88926#problem/C

Description

The city where Georgie lives has n junctions some of which are connected by bidirectional roads. Every day Georgie drives from his home to work and back. But the roads in the city where Georgie lives are very bad, so they are very often closed for repair. Georgie noticed that when some roads are closed he still can get from home to work in the same time as if all roads were available. But there are such roads that if they are closed for repair the time Georgie needs to get from home to work increases, and sometimes Georgie even cannot get to work by a car any more. Georgie calls such roads important. Help Georgie to find all important roads in the city.

Input

The first line of the input file contains n and m — the number of junctions and roads in the city where Georgie lives, respectively (2 ≤ n ≤ 20 000, 1 ≤ m ≤ 100 000). Georgie lives at the junction 1 and works at the junction n. The following m lines contain information about roads. Each road is specified by the junctions it connects and the time Georgie needs to drive along it. The time to drive along the road is positive and doesn’t exceed 100 000. There can be several roads between a pair of junctions, but no road connects a junction to itself. It is guaranteed that if all roads are available, Georgie can get from home to work.

Output

Output l — the number of important roads — at the first line of the output file. The second line must contain l numbers, the numbers of important roads. Roads are numbered from 1 to m as they are given in the input file.

Sample Input

6 7
1 2 1
2 3 1
2 5 3
1 3 2
3 5 1
2 4 1
5 6 2

Sample Output

2
5 7

HINT

题意

给你一个无向图,问你里面有多少个important道路

重要的道路就是指这条路去掉之后,整个图的最短路长度会发生变化

题解

跑tarjan之后,如果这个边是桥的话,就输出就好了

和codeforces #314的E题几乎一模一样

代码:

#include <cstdio>
#include <algorithm>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
const int maxn = ;
#define INF (1LL<<61)
typedef long long ll; struct Dijkstra {
struct node {
ll d;
int u;
bool operator < (const node& b) const {
return d > b.d;
}
node() {}
node(ll _d, int _u): d(_d), u(_u) {}
}; struct Edge {
int from, to, id;
ll dist;
Edge() {}
Edge(int u, int v, ll w) : from(u), to(v), dist(w){}
};
int n, m;
vector<Edge> edges;
vector<int> G[maxn];
bool done[maxn];
ll 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, ll dist) {
edges.push_back(Edge(from, to, dist));
m = edges.size();
G[from].push_back(m-);
} void dijkstra(int s) {
priority_queue<node> Q;
for (int i = ; i <= n; i++) d[i] = INF;
d[s] = ;
memset(done, , sizeof(done));
Q.push(node(, s));
while (!Q.empty()) {
node 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(node(d[e.to], e.to));
}
}
}
}
} S, T; int dfn[maxn]; // 时间戳
int dfs_clock; // dfs时间变量
int low[maxn]; // u及u的后代在DFS树上能够到达的最早的祖先 struct Edge {
int u, v, w, id;
Edge(int a=, int b=, int w=, int c=) : u(a), v(b), w(w), id(c) {}
} e[*maxn]; vector<Edge> G[maxn];
bool isbridge[*maxn]; int dfs(int u, int la) {
int lowu = dfn[u] = ++dfs_clock; // dfs_clock在调用dfs前要初始化为0
int child = ; // 子节点个数
for (int i = ; i < G[u].size(); i++) {
int v = G[u][i].v;
if (!dfn[v]) { // 未访问过,树边
int lowv = dfs(v, G[u][i].id);
lowu = min(lowu, lowv);
if (lowv > dfn[u]) { // 判断桥
isbridge[G[u][i].id] = ;
}
}
else if (dfn[v] < dfn[u] && G[u][i].id != la) { // 反向边
lowu = min(lowu, dfn[v]);
}
}
low[u] = lowu;
return lowu;
} int ison[*maxn];
int can[*maxn];
vector<int> ans;
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;
S.init(n+);
T.init(n+);
int u, v, w;
for (int i = ; i <= m; i++){
scanf("%d%d%d", &u, &v, &w);
e[i*-] = Edge(u, v, w, i*-);
S.addEdge(u, v, w);
T.addEdge(v, u, w);
e[i*] = Edge(v,u,w,i*);
S.addEdge(v,u,w);
T.addEdge(u,v,w);
}
m*=;
S.dijkstra(s);
T.dijkstra(t);
ll ddd = S.d[t];
ll delta;
for (int i = ; i <= m; i++) {
u = e[i].u;
v = e[i].v;
w = e[i].w;
if (S.d[u] + w == S.d[v] && T.d[v] + w == T.d[u]) {
G[u].push_back(Edge(u, v, w, i));
G[v].push_back(Edge(v, u, w, i));
ison[i] = ;
}
}
dfs(s, -); for (int i = ; i <= m; i++) {
if (isbridge[i]) {
ans.push_back((i+)/);
}
} sort(ans.begin(),ans.end());
ans.erase(unique(ans.begin(),ans.end()),ans.end()); printf("%d\n",ans.size());
for(int i=;i<ans.size();i++)
printf("%d ",ans[i]);
printf("\n"); return ;
}

Codeforces Gym 100338C C - Important Roads tarjan的更多相关文章

  1. Codeforces GYM 100876 J - Buying roads 题解

    Codeforces GYM 100876 J - Buying roads 题解 才不是因为有了图床来测试一下呢,哼( 题意 给你\(N\)个点,\(M\)条带权边的无向图,选出\(K\)条边,使得 ...

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

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

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

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

  4. codeforces GYM 100114 J. Computer Network tarjan 树的直径 缩点

    J. Computer Network Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100114 Des ...

  5. ACdream 1415 Important Roads

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

  6. Codeforces Gym 101252D&&floyd判圈算法学习笔记

    一句话题意:x0=1,xi+1=(Axi+xi%B)%C,如果x序列中存在最早的两个相同的元素,输出第二次出现的位置,若在2e7内无解则输出-1. 题解:都不到100天就AFO了才来学这floyd判圈 ...

  7. Codeforces Gym 101190M Mole Tunnels - 费用流

    题目传送门 传送门 题目大意 $m$只鼹鼠有$n$个巢穴,$n - 1$条长度为$1$的通道将它们连通且第$i(i > 1)$个巢穴与第$\left\lfloor \frac{i}{2}\rig ...

  8. Codeforces Gym 101623A - 动态规划

    题目传送门 传送门 题目大意 给定一个长度为$n$的序列,要求划分成最少的段数,然后将这些段排序使得新序列单调不减. 考虑将相邻的相等的数缩成一个数. 假设没有分成了$n$段,考虑最少能够减少多少划分 ...

  9. 【Codeforces Gym 100725K】Key Insertion

    Codeforces Gym 100725K 题意:给定一个初始全0的序列,然后给\(n\)个查询,每一次调用\(Insert(L_i,i)\),其中\(Insert(L,K)\)表示在第L位插入K, ...

随机推荐

  1. Ubuntu消息菜单(MessagingMenu)API

    应用程序可以注册在消息菜单里显示消息,它也可以使用全局聊天状态项目. 注册 应用程序要在消息菜单里显示消息,必须满足以下条件: $HOME/.config/indicators/messages/ap ...

  2. ylbtech-SubwayNav(地铁线路导航)-数据库设计

    ylbtech-DatabaseDesgin:ylbtech-SubwayNav(地铁线路导航)-数据库设计 DatabaseName:SubwayNav(地铁线路导航) Type:线路导航 1.A, ...

  3. Codeforces Round #361 (Div. 2) 套题

    A - Mike and Cellphone 问有没有多解,每个点按照给出的序列用向量法跑一遍 #include<cstdio> #include<cstring> #incl ...

  4. web自动化框架之四测试报告的搭建

    现状: 看过前面的文章,楼主用的是python,所以在搭建测试报告这块的时候使用的是unittest+htmlTestRunner:然后发现生成出来的报告,总是有那么不完美的地方,比如想增加图片,比如 ...

  5. 读pomelo的教程-1

    pomelo教程的例子是一个聊天室,包括一个webserver客户端,和一个gameserver的pomelo服务器.这个例子挺好,一个聊天系统逻辑简单,还包括了用户管理,客户端request,服务器 ...

  6. 用原生javascript模拟经典FC游戏公路争霸

    #用原生javascript模拟经典FC游戏公路争霸 前几天看了园子里面的随笔 [原生javascript开发仿微信打飞机小游戏](http://www.cnblogs.com/Mr-Nobody/p ...

  7. HDU-4742 Pinball Game 3D 三维LIS

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4742 题意:求3维的LIS.. 用分治算法搞得,参考了cxlove的题解.. 首先按照x排序,然后每个 ...

  8. lamda表达式相关知识

    lamda表达式写法 dt = datado.SelectDalMeath(sqlStr.ToString()); var x = (from r in dt.AsEnumerable() selec ...

  9. Emacs Lisp 功能扩展集锦

    http://docs.huihoo.com/homepage/shredderyin/emacs_elisp.html Emacs 具有超强的扩展性.这是当今没有任何其它编辑器可以比拟 的强大特点. ...

  10. 第二百六十三天 how can I 坚持

    今天解脱了,放下了,小罗娜,不给力,话说下一步该咋办呢. 鱼不想过双十一自杀了.这都二十二号了,好快. 该把给罗娜说的那些话保存下来.可惜已经删了. 不知道做的对不对,反正就是没缘分,就这样吧. 睡觉 ...