ACdream 1415 Important Roads
Important Roads
Problem 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
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
Source
Manager
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pli pair<long long,int>
#define INF 0x3f3f3f3f3f3fLL
using namespace std;
const int maxn = ;
struct arc{
int to,next;
LL w;
arc(int x = ,LL y = ,int z = -){
to = x;
w = y;
next = z;
}
};
struct edge{
int to,id,next;
edge(int x = ,int y = ,int z = -){
to = x;
id = y;
next = z;
}
};
arc e[maxn*];
edge g[maxn*];
int head[maxn],first[maxn],ans[maxn],tot2,num;
int n,m,tot,x[maxn*],y[maxn*],z[maxn*];
int dfn[maxn],low[maxn],idx;
LL d[][maxn];
void add(int u,int v,LL w){
e[tot] = arc(v,w,head[u]);
head[u] = tot++;
e[tot] = arc(u,w,head[v]);
head[v] = tot++;
}
void add2(int u,int v,int id){
g[tot2] = edge(v,id,first[u]);
first[u] = tot2++;
g[tot2] = edge(u,id,first[v]);
first[v] = tot2++;
}
void dijkstra(int s,LL ds[maxn]){
priority_queue< pli,vector< pli >,greater< pli > >q;
bool done[maxn] = {false};
for(int i = ; i <= n; i++) ds[i] = INF;
ds[s] = ;
q.push(make_pair(ds[s],s));
while(!q.empty()){
int u = q.top().second;
q.pop();
if(done[u]) continue;
done[u] = true;
for(int i = head[u]; ~i; i = e[i].next){
if(ds[e[i].to] > ds[u] + e[i].w){
ds[e[i].to] = ds[u] + e[i].w;
q.push(make_pair(ds[e[i].to],e[i].to));
}
}
}
}
void tarjan(int u,int fa){
dfn[u] = low[u] = ++idx;
bool flag = true;
for(int i = first[u]; ~i; i = g[i].next){
if(g[i].to == fa && flag){
flag = false;
continue;
}
if(!dfn[g[i].to]){
tarjan(g[i].to,u);
low[u] = min(low[u],low[g[i].to]);
if(low[g[i].to] > dfn[u]) ans[num++] = g[i].id;
}else low[u] = min(low[u],dfn[g[i].to]);
}
}
int main(){
while(~scanf("%d %d",&n,&m)){
memset(head,-,sizeof(head));
memset(first,-,sizeof(first));
memset(dfn,,sizeof(dfn));
memset(low,,sizeof(low));
idx = num = tot2 = tot = ;
for(int i = ; i <= m; i++){
scanf("%d %d %d",x+i,y+i,z+i);
add(x[i],y[i],z[i]);
}
dijkstra(,d[]);
dijkstra(n,d[]);
LL tmp = d[][n];
for(int i = ; i <= m; i++){
int u = x[i];
int v = y[i];
if(d[][u] + z[i] + d[][v] == tmp || d[][v] + z[i] + d[][u] == tmp){
add2(u,v,i);
}
}
for(int i = ; i <= n; i++)
if(!dfn[i]) tarjan(i,-);
printf("%d\n",num);
if(num){
for(int i = ; i < num; i++)
printf("%d%c",ans[i],i + == num?'\n':' ');
}
}
return ;
}
ACdream 1415 Important Roads的更多相关文章
- Codeforces Gym 100338C C - Important Roads tarjan
C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...
- Codeforces Gym 100338C Important Roads 最短路+Tarjan找桥
原题链接:http://codeforces.com/gym/100338/attachments/download/2136/20062007-winter-petrozavodsk-camp-an ...
- codeforces Gym 100338C Important Roads (重建最短路图)
正反两次最短路用于判断边是不是最短路上的边,把最短路径上的边取出来建图.然后求割边.注意重边,和卡spfa. 正权,好好的dijkstra不用,用什么spfa? #include<bits/st ...
- Gym - 100338C Important Roads 最短路+tarjan
题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化. 思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图.在新图中找到 ...
- acdream1415(dij+优先队列+桥)
这题好坑,卡SPFA... 无奈只能用dij+优先队列了. 因为好久没有写过代码了,所以今天写dij时候突然觉得复杂度不对,dij+优先队列的复杂度是(n+m)logn,这种复杂度对于稠密图是非常慢! ...
- Tarjan 联通图 Kuangbin 带你飞 联通图题目及部分联通图题目
Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是 ...
- poj 1251 Jungle Roads (最小生成树)
poj 1251 Jungle Roads (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...
- Jungle Roads[HDU1301]
Jungle Roads Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Tota ...
- CSS中"!important"的使用
本篇文章使用最新的IE10以及firefox与chrome测试(截止2013年5月27日22:23:22) CSS的原理: 我们知道,CSS写在不同的地方有不同的优先级, .css文件中的定义 < ...
随机推荐
- Linux 服务具体解释
acpid ACPI(全 称 Advanced Configuration and Power Interface)服务是电源管理接口. 建议全部的笔记本用户开启它. 一些server可能不须要 ac ...
- yolo源码解析(3):视频检测流程
代码在自己电脑中!!!!不在服务器 根据前文所说yolo代码逻辑: ├── examples │ ├── darknet.c(主程序) │ │── xxx1.c │ └── xxx2.c │ ├── ...
- 11.修改WSDL文档
http://localhost:8077/person?wsdl可以由你来控制的.拿Person这个例子来说.
- [HTML] 条件注释判断浏览器
<!--[if !IE]><!--> 除IE外都可识别 <!--<![endif]--><!--[if IE]> 所有的IE可识别 <![e ...
- canvas的自动画图
<!DOCTYPE HTML><html><body> <canvas id="myCanvas" width="200&quo ...
- 深入了解Token认证的来龙去脉
Token 是在服务端产生的,如果前端使用用户名/密码向服务端请求认证,服务端认证成功,那么在服务端会返回 Token 给前端.前端可以在每次请求的时候带上 Token 证明自己的合法地位. 不久 ...
- java中的数组的Arrays工具类的使用
package day04.d1.shuzu; import java.util.Arrays; /** * Arrays 工具类 * @author Administrator * */public ...
- window下svn开机自动启动
- 适配器模式(adapter)C++实现
意图:将一个类的接口转换成客户希望的另一个接口. 适用性:1.你想使用一个已存在的类,而它的接口不符合你的需求. 2.你想创建一个可以复用的类,该类可以与其它不相关的类或不可预见的类协同工作. 类适配 ...
- CI中的验证码
CI中的验证码相对来说使用非常方便直接加载类调用函数以及一些配置,代码如上,比较简单,具体函数可在CI手册的辅助函数参考CAPTCHA辅助函数中查询,CI中的验证码是直接生成验证码图片在你自己创建的C ...