• <fieldset id="8imwq"><menu id="8imwq"></menu></fieldset>
  • <bdo id="8imwq"><input id="8imwq"></input></bdo>
    最新文章專題視頻專題問答1問答10問答100問答1000問答2000關鍵字專題1關鍵字專題50關鍵字專題500關鍵字專題1500TAG最新視頻文章推薦1 推薦3 推薦5 推薦7 推薦9 推薦11 推薦13 推薦15 推薦17 推薦19 推薦21 推薦23 推薦25 推薦27 推薦29 推薦31 推薦33 推薦35 推薦37視頻文章20視頻文章30視頻文章40視頻文章50視頻文章60 視頻文章70視頻文章80視頻文章90視頻文章100視頻文章120視頻文章140 視頻2關鍵字專題關鍵字專題tag2tag3文章專題文章專題2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章專題3
    問答文章1 問答文章501 問答文章1001 問答文章1501 問答文章2001 問答文章2501 問答文章3001 問答文章3501 問答文章4001 問答文章4501 問答文章5001 問答文章5501 問答文章6001 問答文章6501 問答文章7001 問答文章7501 問答文章8001 問答文章8501 問答文章9001 問答文章9501
    當前位置: 首頁 - 科技 - 知識百科 - 正文

    codeforcesRound#241(div2)E解題報告

    來源:懂視網 責編:小采 時間:2020-11-09 08:02:36
    文檔

    codeforcesRound#241(div2)E解題報告

    codeforcesRound#241(div2)E解題報告:E. President's Path time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional.
    推薦度:
    導讀codeforcesRound#241(div2)E解題報告:E. President's Path time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional.

    E. President's Path time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional.

    E. President's Path

    time limit per test

    4 seconds

    memory limit per test

    256 megabytes

    input

    standard input

    output

    standard output

    Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional. Between any pair of cities, there is at most one road. For each road, we know its length.

    We also know that the President will soon ride along the Berland roads from city s to city t. Naturally, he will choose one of the shortest paths from s to t, but nobody can say for sure which path he will choose.

    The Minister for Transport is really afraid that the President might get upset by the state of the roads in the country. That is the reason he is planning to repair the roads in the possible President's path.

    Making the budget for such an event is not an easy task. For all possible distinct pairs s,?t (s?t) find the number of roads that lie on at least one shortest path from s to t.

    Input

    The first line of the input contains integers n,?m (2?≤?n?≤?500, 0?≤?m?≤?n·(n?-?1)?/?2) — the number of cities and roads, correspondingly. Then m lines follow, containing the road descriptions, one description per line. Each description contains three integersxi,?yi,?li (1?≤?xi,?yi?≤?n,?xi?≠?yi,?1?≤?li?≤?106), where xi,?yi are the numbers of the cities connected by the i-th road and li is its length.

    Output

    Print the sequence of integers c12,?c13,?...,?c1n,?c23,?c24,?...,?c2n,?...,?cn?-?1,?n, where cst is the number of roads that can lie on the shortest path from s to t. Print the elements of sequence c in the described order. If the pair of cities s and t don't have a path between them, then cst?=?0.

    Sample test(s)

    input

    5 6
    1 2 1
    2 3 1
    3 4 1
    4 1 1
    2 4 2
    4 5 4
    

    output

    1 4 1 2 1 5 6 1 2 1 


    題目大意:

    給出一個圖,要求求出每個點之間的最短距離的路總條數.


    解法:

    由于需要求出每個點之間的最短距離的路總條數,我們可以將問題拆分為: 1.最短距離; 2.符合最短距離的路的總條數.

    對于問題1,可以用floyd算法可以算出來;

    對于問題2,本題的重點所在,要求出路的總條數,而且不能重復.不能重復的話,我們可以按照每個點來計算,且每個點,我們只計算點周圍的最短路徑的路的條數,這樣就可以防止重復. 因為1條邊不可能統計2次.(詳見代碼).

    代碼:

    #include 
    #include 
    #include 
    #include 
    
    using namespace std;
    
    const int lim = 500000001;
    
    class TMain {
     
     private:
     
     int n, m, f[505][505], dis[505][505], ans[505][505], now[505];
     
     public:
     
     int run() {
     scanf("%d %d", &n, &m);
     for (int i = 1; i <= n; i++)
     for (int j = 1; j <= n; j++)
     if (i == j)
     f[i][j] = dis[i][j] = 0;
     else
     f[i][j] = dis[i][j] = lim;
     for (int i = 1; i <= m; i++) {
     int a, b, c;
     scanf("%d %d %d", &a, &b, &c);
     dis[a][b] = dis[b][a] = f[a][b] = f[b][a] = min(f[a][b], c);
     }
     
     for (int k = 1; k <= n; k++)
     for (int i = 1; i <= n; i++)
     for (int j = 1; j <= n; j++)
     f[i][j] = min(f[i][j], f[i][k] + f[k][j]); //算出每對點之間的最短路徑
    
     for (int i = 1; i <= n; i++) { //從
     for (int j = 1; j <= n; j++) now[j] = 0;
    
     for (int j = 1; j <= n; j++)
     for (int k = 1; k <= n; k++)
     if (j != k && f[i][j] + dis[j][k] == f[i][k]) //逐一判斷與k點鏈接的邊是否在最短路徑上
     now[k]++; //若在,則k節點的路總條數加一
    
    	//由于是最短路,不存在可以重復的邊,例如f[i][j]是一條邊, now[i]和now[j]只能在兩者之間的某一個累加
     for (int j = 1; j <= n; j++)
     for (int k = 1; k <= n; k++)
     if (f[i][j] + f[j][k] == f[i][k]) //如若最短路徑需要經過j點,則可以說明,需要j點的最短路徑
     ans[i][k] += now[j]; //累加起來
     }
     
     for (int i = 1; i < n; i++)
     for (int j = i + 1; j <= n; j++)
     if (f[i][j] == lim)
     printf("0 ");
     else
     printf("%d ", ans[i][j]);
     printf("\n");
     return 0;
     }
    }Main;
    
    int main()
    {
     return Main.run();
    }

    聲明:本網頁內容旨在傳播知識,若有侵權等問題請及時與本網聯系,我們將在第一時間刪除處理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

    文檔

    codeforcesRound#241(div2)E解題報告

    codeforcesRound#241(div2)E解題報告:E. President's Path time limit per test 4 seconds memory limit per test 256 megabytes input standard input output standard output Good old Berland has n cities and m roads. Each road connects a pair of distinct cities and is bidirectional.
    推薦度:
    標簽: 報告 解題 round
    • 熱門焦點

    最新推薦

    猜你喜歡

    熱門推薦

    專題
    Top
    主站蜘蛛池模板: 久久99精品国产自在现线小黄鸭| 精品无人区麻豆乱码1区2区 | 亚洲国产精品国自产电影| 亚洲韩精品欧美一区二区三区| 国产精品免费看久久久香蕉| 国语自产拍精品香蕉在线播放| 久久国产免费观看精品3| 区亚洲欧美一级久久精品亚洲精品成人网久久久久 | 麻豆亚洲AV永久无码精品久久| 欧美成人精品一区二区综合| 国产精品欧美久久久久无广告 | 亚洲精品无码久久久久久| 久久精品亚洲福利| 精品无码久久久久久国产| 国产福利电影一区二区三区,亚洲国模精品一区| 999久久久免费精品国产| 国内精品久久久久久99蜜桃| 无码国产精品一区二区免费vr| 伊人久久精品影院| 中文精品久久久久人妻| 自拍中文精品无码| 亚洲精品色午夜无码专区日韩| 亚洲欧美精品综合中文字幕 | 麻豆国内精品欧美在线| 久久精品无码一区二区日韩AV| 精品一区二区三区四区在线| 国产亚洲精品自在线观看| 国产乱码精品一区二区三| 中文无码久久精品| 国产精品成人99久久久久91gav| 国产精品一区二区久久国产| 亚洲处破女AV日韩精品| 日产欧美国产日韩精品| 日本一卡精品视频免费| 久久亚洲中文字幕精品有坂深雪| 久久发布国产伦子伦精品| 久久久久久夜精品精品免费啦| 国内精品伊人久久久久AV影院| 99国产欧美精品久久久蜜芽| 国产精品久久久久影院嫩草| 久久久久夜夜夜精品国产|