D - Silver Cow Party——最短路_spfa()算法
2017-07-17 15:21
441 查看
Think:
1知识点:最短路_spfa()算法
2反思:图的初始化
3思路:通过正向边spfa()求出回去的,反向边spfa()求出到达的
建议参考题意分析
vjudge题目链接
以下为Accepted代码
1知识点:最短路_spfa()算法
2反思:图的初始化
3思路:通过正向边spfa()求出回去的,反向边spfa()求出到达的
建议参考题意分析
vjudge题目链接
以下为Accepted代码
#include <cstdio> #include <cstring> #include <algorithm> #include <queue> using namespace std; const int inf = 0x3f3f3f3f; const int N = 1e3 + 4; int n, e1 , e2 , dis , vis ; void spfa(int x, int e ); int main(){ int m, i, x, u, v, w, sum , mav; scanf("%d %d %d", &n, &m, &x); mav = 0; memset(sum, 0, sizeof(sum)); memset(e1, inf, sizeof(e1)); memset(e2, inf, sizeof(e2)); for(i = 0; i < m; i++){ scanf("%d %d %d", &u, &v, &w); e1[u][v] = w; e2[v][u] = w; } spfa(x, e1); for(i = 1; i <= n; i++) sum[i] += dis[i]; spfa(x, e2); for(i = 1; i <= n; i++){ mav = max(mav, sum[i]+dis[i]); } printf("%d\n", mav); return 0; } void spfa(int x, int e ){ queue <int> q; memset(dis, inf, sizeof(dis)); memset(vis, 0, sizeof(vis)); dis[x] = 0, vis[x] = 1; q.push(x); while(!q.empty()){ int t1 = q.front(); q.pop(); vis[t1] = 0; for(int i = 1; i <= n; i++){ if(dis[t1] + e[t1][i] < dis[i]){ dis[i] = dis[t1] + e[t1][i]; if(!vis[i]){ q.push(i); vis[i] = 1; } } } } }
相关文章推荐
- Silver Cow Party SPFA+SLF优化 单源起点终点最短路
- 暑假-最短路(Bellman-ford、spfa)-E - Silver Cow Party
- 文章标题 POJ 3268 : Silver Cow Party (最短路--spfa)
- 【算法系列学习】Dijkstra求最短路 [kuangbin带你飞]专题四 最短路练习 D - Silver Cow Party
- POJ3268 Silver Cow Party spfa求解 最短路
- poj 3268 Silver Cow Party(最短路+SPFA)
- poj 3268 Silver Cow Party(最短路+SPFA)
- poj-3268 Silver Cow Party (最短路-spfa-求到同一起点,同一终点的最短路)
- poj3268 Silver Cow Party(最短路变形)
- poj 3268 Silver Cow Party (最短路算法的变换使用 【有向图的最短路应用】 )
- POJ 3268 Silver Cow Party 最短路
- POJ 3268 Silver Cow Party(最短路)
- POJ 3268 Silver Cow Party 最短路
- POJ - 3268 Silver Cow Party (逆向最短路 多源最短路)
- POJ Silver Cow Party【最短路】
- poj 3268 Silver Cow Party(最短路矩阵转置~)
- POJ 3268 Silver Cow Party(最短路 dijkstra求任意两点最短路)
- POJ 3268 Silver Cow Party(最短路dijkstra)
- BZOJ1631: [Usaco2007 Feb]Cow Party 最短路 Spfa
- poj3268 - Silver Cow Party (两次最短路)