codeforces 416E. President's Path( graph dp)

http://codeforces.com/problemset/problem/416/E

题目:

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 citys to city t. Naturally, he will choose one of the shortest paths froms 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 pairss, t (s < t) find the number of roads that lie on at least one shortest path froms 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. Thenm 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 thei-th road and li is its length.

Output

Print the sequence of integersc12, c13, ..., c1n, c23, c24, ..., c2n, ..., cn - 1, n, where cst is the number of roads that can lie on the shortest path froms to t. Print the elements of sequencec in the described order. If the pair of citiess and t don't have a path between them, thencst = 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 

用floyd动态思想解决此问题,附带一张图说明:

先对每两点floyd最短化,然后记录能到达终点的位置点作为中点,统计总的线路。取中点的地方细细品味。。。

#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int N=130000,M=505,INF=1<<29;
int map[M][M],dp[M],ans[M][M];
struct node{
    int u,v,len;
}edge[N];
void init(){
    for(int i=1;i<M;i++){
       for(int j=1;j<M;j++){
           if(i==j) map[i][j]=0;
           else map[i][j]=INF;
       }
    }
    memset(ans,0,sizeof(ans));
}
int main(){
    //freopen("cin.txt","r",stdin);
    int n,m;//1<<30=1073741824  1<<29=536870912
    int x,y,p;
    while(cin>>n>>m){
         init();
         for(int i=0;i<m;i++){
            scanf("%d%d%d",&x,&y,&p);
            edge[i].u=x;
            edge[i].v=y;
            edge[i].len=p;
            if(p<map[x][y]){
                map[x][y]=map[y][x]=p;
            }
         }
         for(int k=1;k<=n;k++){//floyd 最短化
             for(int i=1;i<=n;i++){
                  for(int j=1;j<=n;j++){//涉及端点
                      map[i][j]=min(map[i][j],map[i][k]+map[k][j]);
                  }//1<<30能导致溢出
             }
          }
          /*for(int i=1;i<=n;i++){           
               for(int j=1;j<=n;j++) cout<<map[i][j]<<" ";  cout<<endl;        }*/
          for(int i=1;i<=n;i++){// 起点
               memset(dp,0,sizeof(dp));
               for(int j=0;j<m;j++){//中点 (也包含本身两个端点的情况)
                    if(map[edge[j].u][i]==edge[j].len+map[edge[j].v][i])
                    dp[edge[j].u]++;
                    if(map[edge[j].v][i]==edge[j].len+map[edge[j].u][i])
                    dp[edge[j].v]++;
               }
               for(int j=i+1;j<=n;j++){//终点
                    for(int k=1;k<=n;k++){//中点
                        if(map[i][j]==map[i][k]+map[k][j])  //相等即能到达。
                        ans[i][j]+=dp[k];
                    }
                    if(i==n-1&&j==n) printf("%d\n",ans[i][j]);
                    else printf("%d ",ans[i][j]);
               }
         }
     }
     return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值