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文件中的定义 < ...
随机推荐
- C语言高速入门系列(一)
C语言高速入门系列(一) 本系列引言: 本教程的宗旨是将C语言入门的内容进行关键知识点的提纯,将一些笼统的废话去除; 再进行压缩,然后将本章的关键知识点做成路线图的,能够更加方便地掌握学习的方向; ...
- makepy
文件连接: https://files.cnblogs.com/files/mophy/%E7%99%BB%E5%BD%95%E6%B5%81%E7%A8%8B%E5%88%86%E6%9E%90.7 ...
- 【NOIP2011 Day 2】观光公交
[问题描述] 小城Y市,拥有n个景点.由于慕名而来的游客越来越多,Y市特意安排了一辆观光公交车,为游客提供更便捷的交通服务.观光公交车在第0分钟出现在1号景点,随后依次前往2.3.4……n号景点.从第 ...
- 关于H5优化的一些问题
required修改默认提示 : <form action="abc.php"> <input type="text" required o ...
- Spring生态简介
目录 概述 项目说明 主要项目 社区项目 保留项目 最后总结 概述 做Java开发的人一提起Spring,首先在脑海中浮现出的就是"IoC","AOP",&qu ...
- SSRS 报表 如何加参数
SSRS 报表 如何加参数 连接上以后出现一个问题 就是给报表加上参数以后报表不断刷新,跟上次那个报表刷新是同样的问题.那么下面我们来解决一下. 1. 这是给报表添加默认参数进入页面后就不断的刷新刷新 ...
- ansible upload
# 链接地址:https://www.cnblogs.com/xiaoxiaoleo/p/6626299.html # synchronize: 从拉取远程服务器文件,需要加mode: pull # ...
- ffmpeg编码
1. 注册所有容器格式和CODEC:av_register_all()2. 打开文件:av_open_input_file()3. 从文件中提取流信息:av_find_stream_info()4. ...
- 【MFC】基于opencv的趣味相机
为了参加学校的科技节,故用mfc随手制作了一个名为<趣味相机>的小程序: 其中对图形图像处理运用到了opencv. 效果图 这界面逼格低了点╭(╯^╰)╮ 有兴趣的朋友可以在此下载尝试:h ...
- (转载) popupWindow 指定位置上的显示
popupWindow 指定位置上的显示 标签: androidpopupWindowpopupWindow具体位置放置 2014-07-09 16:23 1114人阅读 评论(0) 收藏 举报 分 ...