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.
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.
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.
5 61 2 12 3 13 4 14 1 12 4 24 5 4
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;}
- codeforces 416E. President's Path( graph dp)
- CodeForces 416E President's Path
- Codeforces 416E President's Path
- CF 416E - President's Path(floyd + DP)
- codeforces 416E President's Path floyd+D(递)P(推)
- Codeforces 416E. President's Path 图论 最短路 处理技巧
- Codeforces 459E Pashmak and Graph【Dp】
- codeforces 459E E. Pashmak and Graph(dp)
- codeforces 459E E. Pashmak and Graph(dp)
- Codeforces 567 E. President and Roads
- Codeforces 567E President and Roads
- codeforces 567E President and Roads Dijikstra
- Codeforces 567E President and Roads 题解
- Codeforces 416E DP
- Codeforces 459E Pashmak and Graph(dp+贪心)
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph【DP】
- Codeforces Round #261 (Div. 2) E. Pashmak and Graph DP
- Codeforces 459E. Pashmak and Graph (DP)
- 固定SessionID 漏洞 攻击(session fixation attacks)
- Android版添加phonegap-读取手机短信插件教程
- 给元素绑定事件时传参
- 使用Touch ID指纹解锁
- 1-1-绪论-第1章-《数据结构》课本源码-严蔚敏吴伟民版
- codeforces 416E. President's Path( graph dp)
- 文章标题
- Web自动化测试原理
- JavaScript基础——使用Canvas画图
- Spring controller
- 关于Build AutoMatically的一些解决办法
- hive使用技巧(三)——巧用group by实现去重统计
- web.xml 中的过滤器(拦截器)Filter与监听器Listener的作用和区别?
- iOS 学会使用delegate和block<二>