Codeforces 416E. President's Path 图论 最短路 处理技巧
来源:互联网 发布:西安软件开发 大专 编辑:程序博客网 时间:2024/05/18 01:32
题目
题目链接:http://codeforces.com/problemset/problem/416/E
题目来源:寒假总结赛王老板出的题。
简要题意:给定一张图,求出有多少边至少属于一条
i,j(i<j) 间的最短路。
题解
这是一道图论的难题,需要想到整个处理的技巧才行。关键一点是最短路的子路也是最短路。
这题首先需要预处理下Floyd。
然后求出
i→j 的最短路中有多少指向j 的边cnt[i][j] ,用δ(i,j) 表示i,j 间的最短路。
δ(i,k)=e(k,j)+δ(i,j) 则我们可以确定k→j 的边一定是在最短路上的。然后我们枚举所有点判断下
cnt 就做出来了。若
δ(i,j)=δ(i,k)+δ(k,j) 则i→k→j 可以产生最短路。此时我们加上
cnt[i][k] 就是把以k 为终点的边加上,枚举k 就能得到结果。
代码
#include <iostream>#include <cstdio>#include <cmath>#include <algorithm>#include <cstring>#include <stack>#include <queue>#include <string>#include <vector>#include <set>#include <map>#define fi first#define se secondusing namespace std;typedef long long LL;typedef pair<int,int> PII;// headconst int N = 505;const int INF = 0x3f3f3f3f;int e[N][N];int dis[N][N];int cnt[N][N];void Floyd(int n) { for (int i = 0; i < n; i++) { dis[i][i] = 0; } for (int k = 0; k < n; k++) { for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { dis[i][j] = min(dis[i][j], dis[i][k] + dis[k][j]); } } }}int main() { memset(e, INF, sizeof e); int n, m, u, v, c; scanf("%d%d", &n, &m); for (int i = 0; i < m; i++) { scanf("%d%d%d", &u, &v, &c); u--, v--; e[u][v] = e[v][u] = c; } memcpy(dis, e, sizeof dis); Floyd(n); // 处理出指向j的边中在i->j最短路上的个数 // 注意判断条件 for (int i = 0; i < n; i++) { for (int j = 0; j < n; j++) { for (int k = 0; k < n; k++) { if (e[k][j] != INF && dis[i][k] + e[k][j] == dis[i][j]) { cnt[i][j]++; } } } } // 由于最短路的传递性,i->k k->j也为最短路 // 由于做出来是入的cnt,因而枚举点作为入的点 for (int i = 0; i < n; i++) { for (int j = i+1; j < n; j++) { int ans = 0; for (int k = 0; k < n; k++) { if (dis[i][k] + dis[k][j] == dis[i][j]) { ans += cnt[i][k]; } } printf("%d ", ans); } } return 0;}
0 0
- Codeforces 416E. President's Path 图论 最短路 处理技巧
- CodeForces 416E President's Path
- Codeforces 416E President's Path
- codeforces 416E. President's Path( graph dp)
- CodeForces 567E President and Roads(最短路 + tarjan)
- Codeforces 567E President and Roads (最短路 好题)
- Codeforces 567E President and Roads 【最短路】
- codeforces 416E President's Path floyd+D(递)P(推)
- CF 416E - President's Path(floyd + DP)
- Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)
- CodeForces 567E President and Roads(最短路 + tarjan求桥)
- Codeforces Round #Pi (Div. 2) E. President and Roads(边双无向图缩点(有重边)+最短路)
- Codeforces Round #Pi (Div. 2) E. President and Roads 最短路,桥
- 08/13 D -> codeforces Round #Pi div 2 E. President and Roads 最短路变形
- Codeforces 567E President and Roads (用Dijkstra求最短路条数 + 强连通桥)
- CodeForces 567E President and Roads(最短路 + 双联通分量)
- CodeForces 59E Shortest Path 用边跑最短路
- 最短路Codeforces E. Breaking Good
- Clob 转String 类型使用 以及转码问题
- iphone分辨率终极指南(含iphone6/6+)
- system阻塞SIGCHLD信号原因
- Socket使用教程——AsyncSocket
- iOS9下有关CoreLocation学习三
- Codeforces 416E. President's Path 图论 最短路 处理技巧
- 《数据结构与算法分析(c 描述)》—— 第六章笔记
- NS2在eclipse下的部署以及调试
- xcode 调试
- 华尔街见闻-2016年2月
- Android allowTaskReparenting clearTaskOnLaunch alwaysRetainTaskState
- ELK(ElasticSearch, Logstash, Kibana)搭建实时日志分析平台
- hdu4521 小明系列问题——小明序列(条件LIS)
- linux grep命令