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

来源:互联网 发布:linux查看端口nc 编辑:程序博客网 时间:2024/04/30 18:41

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 cityt. Naturally, he will choose one of the shortest paths froms tot, 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 andli 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 tot. Print the elements of sequencec in the described order. If the pair of citiess andt don't have a path between them, thencst = 0.

Sample test(s)
Input
5 61 2 12 3 13 4 14 1 12 4 24 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 宝宝吃奶粉上火便秘怎么办 20多天的宝宝便秘怎么办 宝宝五十天消化不良不拉屎怎么办 一周七个月宝宝消化不良拉屎怎么办 孩子总是消化不良拉屎不成型怎么办 一岁宝宝长牙慢怎么办 一岁宝宝不喝水怎么办 七个月的宝宝咳嗽怎么办 怀孕七个月感冒了怎么办 苹果汁弄到白色衣服怎么办 宝宝大便干燥拉不出来怎么办 婴儿吃过青菜米粉呕吐怎么办 50天宝宝便秘5天怎么办 8个月婴幼儿便秘怎么办 1个月婴幼儿便秘怎么办 榨汁后的苹果渣怎么办 宝宝6个月后容易生病怎么办 婴儿头型睡偏了怎么办 6个月婴儿不吃奶怎么办 宝宝秋季腹泻反复发烧怎么办 纯甄过期一个月怎么办 四个月宝宝不吃奶瓶怎么办 四个月宝宝不吃奶粉怎么办 婴儿三四天不大便怎么办 婴儿几天不拉大便怎么办 新生儿6天不拉屎怎么办 孩子10天不大便怎么办 宝宝5天没拉屎怎么办 小孩4-5天不大便怎么办 三个月宝宝睡前哭闹厉害怎么办 新生儿抱着睡放下就醒怎么办 宝宝大运动发育迟缓怎么办 八个月宝宝不会爬怎么办 宝宝竖着抱早了怎么办 孕妇吃的水果少怎么办 6个月宝宝厌奶怎么办 5个月宝宝厌奶怎么办 11个月宝宝厌奶怎么办 母乳宝宝不喝奶粉怎么办 换奶粉不拉大便怎么办 宝宝和妈妈不亲怎么办