ASC(22)C(最短路+雙連通分量找橋或拓撲排序)

來源:互聯網
上載者:User

標籤:acm   asc   

Important RoadsSpecial JudgeTime Limit: 20000/10000MS (Java/Others)Memory Limit: 128000/64000KB (Java/Others)SubmitStatisticNext ProblemProblem 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 71 2 12 3 12 5 31 3 23 5 12 4 15 6 2
Sample Output
25 7

題意:給出一個無向圖,起點為1,終點為n,要求的是找出一些邊,這些邊滿足,刪掉以後從起點到終點的最短路的長度會變小
思路:先求出起點到各個點的最短路,用dj+堆最佳化,spfa不穩定很噁心
            然後可以根據各個點的最短路的值處理出最短路的圖,這個圖裡所有的邊都是最短路上的邊且滿足任何點都能走到終點
            處理出了這個圖以後有兩種做法
            1.可以將最短路圖處理成無向圖,然後用雙連通分量找橋即可,橋就是題目要找的邊,因為刪掉以後一定不能再從起點走到終點
            2.將最短路圖處理成單向的,就是只有起點往終點的方向,不難發現這是一個拓撲圖,然後按拓撲排序的順序遍曆這張圖
               設定一個全域變數,記錄的是所有點的出度與入度的差值,如果當前這個差值為1,那麼當前的點所指向的邊一定是題目要找的邊
               這個方法是隊友想出來的 Orz...
            下面是兩種方法的代碼
方法1:
方法2:

ASC(22)C(最短路+雙連通分量找橋或拓撲排序)

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.