Important Roads

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

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

      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

Source

Andrew Stankevich Contest 22

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的更多相关文章

  1. Codeforces Gym 100338C C - Important Roads tarjan

    C - Important RoadsTime Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://acm.hust.edu.cn/vjudge/contes ...

  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. Gym - 100338C Important Roads 最短路+tarjan

    题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化. 思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图.在新图中找到 ...

  5. acdream1415(dij+优先队列+桥)

    这题好坑,卡SPFA... 无奈只能用dij+优先队列了. 因为好久没有写过代码了,所以今天写dij时候突然觉得复杂度不对,dij+优先队列的复杂度是(n+m)logn,这种复杂度对于稠密图是非常慢! ...

  6. Tarjan 联通图 Kuangbin 带你飞 联通图题目及部分联通图题目

    Tarjan算法就不说了 想学看这 https://www.byvoid.com/blog/scc-tarjan/ https://www.byvoid.com/blog/biconnect/ 下面是 ...

  7. poj 1251 Jungle Roads (最小生成树)

    poj   1251  Jungle Roads  (最小生成树) Link: http://poj.org/problem?id=1251 Jungle Roads Time Limit: 1000 ...

  8. Jungle Roads[HDU1301]

    Jungle Roads Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Tota ...

  9. CSS中"!important"的使用

    本篇文章使用最新的IE10以及firefox与chrome测试(截止2013年5月27日22:23:22) CSS的原理: 我们知道,CSS写在不同的地方有不同的优先级, .css文件中的定义 < ...

随机推荐

  1. Sql Server 强制断开数据库已有连接的方法

    用管理员账户sa登陆,然后在master下新建查询: 在查询窗体输入: declare @i int declare cur cursor for select spid from sysproces ...

  2. 关于Linux静态库和动态库的分析

    关于Linux静态库和动态库的分析 关于Linux静态库和动态库的分析 1.什么是库 在windows平台和linux平台下都大量存在着库. 本质上来说库是一种可运行代码的二进制形式.能够被操作系统加 ...

  3. iOS音频播放之AudioQueue(一):播放本地音乐

    AudioQueue简单介绍 AudioStreamer说明 AudioQueue具体解释 AudioQueue工作原理 AudioQueue主要接口 AudioQueueNewOutput Audi ...

  4. 有关java.util.ConcurrentModificationException

    有关java.util.ConcurrentModificationException java doc对这个类的定义: This exception may be thrown by methods ...

  5. ssh无法连接到远端Ubuntu的解决方法

    近日,饱受无法远程登录到新安装在VMWare上的Ubuntu虚拟机,现在发现问题所在.故记录此问题的解决方式,以备后用. 一.远程登录虚拟机的准备: Ubuntu虚拟机的联网方式应该选择Bridged ...

  6. Android开发之BUG专讲:入门篇(一)

    前言: 本文作者:周才智 转载须注明作者与出处.违者必究. 原文地址:http://segmentfault.com/a/1190000004380690 话说诸葛亮是一个优秀的程序员,每个锦囊都是应 ...

  7. ScrollViewer滚动究竟来触发载入数据的Behavior

    近期项目中遇到载入数据的性能问题, 原因是.net4.0的虚拟化支持不够完毕,有好多bug在4.5才修复. 我们仅仅能利用大家通用的做法来延迟载入数据: 每次载入固定少量的数据.当拖动究竟后.继续载入 ...

  8. hdoj--2509--Be the Winner(尼姆博弈)

    Be the Winner Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) To ...

  9. 【NOIP 2011】 计算系数

    [题目链接] https://www.luogu.org/problemnew/show/P1313 [算法] 二项式定理 [代码] #include<bits/stdc++.h> usi ...

  10. N数码问题的启发式搜索算法--A*算法python实现

    一.启发式搜索:A算法 1)评价函数的一般形式 : f(n) = g(n) + h(n) g(n):从S0到Sn的实际代价(搜索的横向因子) h(n):从N到目标节点的估计代价,称为启发函数(搜索的纵 ...