首页 > 代码库 > ASC(22)C(最短路+双连通分量找桥或拓扑排序)

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:
<script src="https://code.csdn.net/snippets/479429.js" type="text/javascript"></script>

方法2:
<script src="https://code.csdn.net/snippets/479428.js" type="text/javascript"></script>

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