president's path(floyd+dp)

题目:

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 sto 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 integers xi, 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.

Examples

Input

扫描二维码关注公众号,回复: 2484661 查看本文章
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 

题目大意:给定 n 个点 m 条边的无向图,求出对于所有的 pair(s,t),图中有多少条边位于 s 到 t 的最短路径上。

分析:先用floyd算法求出每对节点的最短路径,之后需要计算每条最短路径上有多少条边。dp[i][k]表示有多少条与i相邻的边在i到k上。最后统计这条路径上总共有多少条边就可以了。(i,j,k)顺序对结果应该没影响。

#include <iostream>
#include <stdio.h>
#include <algorithm>
#include <string.h>
#define maxn 505
#define INF 0x3f3f3f3f
using namespace std;
int n,m;
int dist[maxn][maxn];
int c[maxn][maxn];
int dp[maxn][maxn];
void Floyd()
{
    for(int i=1;i<=n;i++)
        dist[i][i]=0;
    for(int k=1;k<=n;k++)
    for(int i=1;i<=n;i++)
        for(int j=1;j<=n;j++)
        dist[i][j]=min(dist[i][j],dist[i][k]+dist[k][j]);
}
int main()
{
   while(scanf("%d%d",&n,&m)==2)
   {
       int u,v,w;
       memset(c,INF,sizeof(c));
      // memset(dist,0,sizeof(dist));
      // memset(dp,0,sizeof(dp));
       while(m--)
       {
           scanf("%d%d%d",&u,&v,&w);
          c[u][v]=c[v][u]=w;
       }
       memcpy(dist,c,sizeof(c));
        Floyd();
        //多少条指向k的边在i->k上。
        for(int i=1;i<=n;i++)
            for(int k=1;k<=n;k++)
            for(int j=1;j<=n;j++)
            {
                if(c[i][k]!=INF&&dist[i][j]==dist[k][j]+c[i][k])//k是否是最短路径上的一个中间节点。
                    ++dp[i][j];//有多少条与i相邻的边在i到k最短路径上。
            }
            // 由于最短路的传递性,i->k k->j也为最短路
            // 由于做出来是入的dp,因而枚举点作为入的点
            for(int i=1;i<=n;i++)
                for(int j=i+1;j<=n;j++)
                {
                    int ans=0;
                    for(int k=1;k<=n;k++)//在i->j这条路上枚举所有的k;如果他可以构成最短路就说明他是一条边。
                        if(dist[i][j]==dist[i][k]+dist[k][j])
                        ans+=dp[k][j];
                    printf("%d%c",ans,i!=n-1?' ':'\n');
                }
   }
    return 0;
}

猜你喜欢

转载自blog.csdn.net/guagua_de_xiaohai/article/details/81210899