ASC(22)C(最短路+双连通分量找桥或拓扑排序)

Important Roads

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

SubmitStatisticNext
Problem

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

题意:给出一个无向图,起点为1,终点为n,要求的是找出一些边,这些边满足,删掉以后从起点到终点的最短路的长度会变小

思路:先求出起点到各个点的最短路,用dj+堆优化,spfa不稳定很恶心

然后可以根据各个点的最短路的值处理出最短路的图,这个图里所有的边都是最短路上的边且满足任何点都能走到终点

处理出了这个图以后有两种做法

1.可以将最短路图处理成无向图,然后用双连通分量找桥即可,桥就是题目要找的边,因为删掉以后一定不能再从起点走到终点

2.将最短路图处理成单向的,就是只有起点往终点的方向,不难发现这是一个拓扑图,然后按拓扑排序的顺序遍历这张图

设置一个全局变量,记录的是所有点的出度与入度的差值,如果当前这个差值为1,那么当前的点所指向的边一定是题目要找的边

这个方法是队友想出来的 Orz...

下面是两种方法的代码

方法1:

方法2:

时间: 2024-12-21 11:59:41

ASC(22)C(最短路+双连通分量找桥或拓扑排序)的相关文章

hdoj 4738 Caocao's Bridges【双连通分量求桥】

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 3000    Accepted Submission(s): 953 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. Bu

POJ 3352 Road Construction(边双连通分量,桥,tarjan)

题解转自http://blog.csdn.net/lyy289065406/article/details/6762370   文中部分思路或定义模糊,重写的红色部分为修改过的. 大致题意: 某个企业想把一个热带天堂岛变成旅游胜地,岛上有N个旅游景点,保证任意2个旅游景点之间有路径连通的(可间接连通).而为了给游客提供更方便的服务,该企业要求道路部门在某些道路增加一些设施. 道路部门每次只会选择一条道路施工,在该条道路施工完毕前,其他道路依然可以通行.然而有道路部门正在施工的道路,在施工完毕前是

zoj 2588 Burning Bridges【双连通分量求桥输出桥的编号】

Burning Bridges Time Limit: 5 Seconds      Memory Limit: 32768 KB Ferry Kingdom is a nice little country located on N islands that are connected by M bridges. All bridges are very beautiful and are loved by everyone in the kingdom. Of course, the sys

hdoj 4612 Warm up【双连通分量求桥&&缩点建新图求树的直径】

Warm up Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65535/65535 K (Java/Others)Total Submission(s): 5093    Accepted Submission(s): 1131 Problem Description N planets are connected by M bidirectional channels that allow instant transport

hdu4738(边双连通分量,桥)

Caocao's Bridges Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 5595    Accepted Submission(s): 1757 Problem Description Caocao was defeated by Zhuge Liang and Zhou Yu in the battle of Chibi. B

hdu Caocao's Bridges(无向图边双连通分量,找出权值最小的桥)

1 /* 2 题意:给出一个无向图,去掉一条权值最小边,使这个无向图不再连同! 3 4 tm太坑了... 5 1,如果这个无向图开始就是一个非连通图,直接输出0 6 2,重边(两个节点存在多条边, 权值不一样) 7 3,如果找到了桥的最小权值为0,也就是桥上的士兵数为0,那么还是要最少派一个 8 士兵过去炸掉桥! 9 10 思路:假设每两个节点最多只有一条边进行相连! 11 进行tarjan算法,如果该算法调用了超过2次,说明这个原图就是不连通的! 12 否则在tarjan算法中将桥存起来!然后

UVA 1364 - Knights of the Round Table (找双连通分量 + 二分图染色法判断)

都特么别说话,我先A了这道题! 卧槽啊.....! 题意来自 kuangbin: 亚瑟王要在圆桌上召开骑士会议,为了不引发骑士之间的冲突, 并且能够让会议的议题有令人满意的结果,每次开会前都必须对出席会议的骑士有如下要求: 1.  相互憎恨的两个骑士不能坐在直接相邻的2个位置: 2.  出席会议的骑士数必须是奇数,这是为了让投票表决议题时都能有结果. 注意:1.所给出的憎恨关系一定是双向的,不存在单向憎恨关系. 2.由于是圆桌会议,则每个出席的骑士身边必定刚好有2个骑士. 即每个骑士的座位两边都

[HDOJ4612]Warm up(双连通分量,缩点,树直径)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4612 所有图论题都要往树上考虑 题意:给一张图,仅允许添加一条边,问能干掉的最多条桥有多少. 必须解决重边的问题,最后会说. 首先tarjan跑出所有的双连通分量和是桥的边还有桥的数量,这非常重要.接着缩点重新建图,然后两遍dfs找出两个在树上距离最远的点.我的想法就是把这条最长的链连成一个环,让它成为一个双连通分量,这样的效果是最好的.最后就是用桥的数量减去树直径再减一就得到了剩下的桥的数量了.求

POJ 2942 Knights of the Round Table (点双连通分量,偶图判定)

题意:多个骑士要开会,3人及以上才能凑一桌,其中部分人已经互相讨厌,肯定不坐在同一桌的相邻位置,而且一桌只能奇数个人才能开台.给出多个人的互相讨厌图,要求多少人开不成会(注:会议不要求同时进行,一个人开多个会不冲突)? 分析: 给的是互相讨厌的图,那么转成互相喜欢的吧,扫一遍,如果不互相讨厌就认为互相喜欢,矩阵转邻接表先. 有边相连的两个点代表能坐在一块.那么找出一个圈圈出来,在该圈内的点有奇数个人的话肯定能凑成1桌.圈圈?那就是简单环了,跟点双连通分量的定义好像一样:每个点都能同时处于1个及以