UVA 10000 Longest Paths(SPFA)
来源:互联网 发布:电子贺卡制作软件 编辑:程序博客网 时间:2024/05/16 11:13
题意是让求最长路,和最短路思想一样
代码是套的kuangbin的SPFA的模板
#include <iostream>#include <vector>#include <queue>#include <cstring>using namespace std;const int MAXN = 110;struct Edge{ int v,cost; Edge(int _v = 0, int _cost = 0):v(_v),cost(_cost){}};vector<Edge> E[MAXN];void addEdge(int u, int v, int w){ E[u].push_back(Edge(v,w));}bool vis[MAXN];bool cnt[MAXN];int dist[MAXN];bool SPFA(int start, int n){ memset(cnt,0,sizeof(cnt)); memset(vis,false,sizeof(vis)); memset(dist,0,sizeof(dist)); vis[start] = true; dist[start] = 0; queue<int> que; que.push(start); cnt[start] = 1; while(!que.empty()) { int u = que.front(); que.pop(); vis[u] = false; for(int i = 0; i < E[u].size(); ++i) { int v = E[u][i].v; if(dist[v] < dist[u] + E[u][i].cost) { dist[v] = dist[u] + E[u][i].cost; if(!vis[v]) { vis[v] = true; que.push(v); if(++cnt[v] > n) return false; } } } } return true;}int main(){ int n,start,time = 0; while(cin >> n && n) { ++time; memset(E,0,sizeof(E)); cin >> start; int u,v; while(cin >> u >> v && (u+v)) addEdge(u,v,1); SPFA(start,n); int res = 0,ires = 0; for(int i = 1; i <= n; ++i) { if(dist[i] > res) { res = dist[i]; ires = i; } } cout << "Case " << time << ": The longest path from " << start << " has length " << res << ", finishing at " << ires << "." << endl << endl; } return 0;}
0 0
- uva 10000 Longest Paths (SPFA)
- UVA 10000 Longest Paths(SPFA)
- UVA 10000 -Longest Paths-Bellman - Ford,flody,spfa
- UVA-10000 Longest Paths (SPFA最短路算法)
- UVa 10000 - Longest Paths
- UVA 10000 - Longest Paths
- Uva Oj 10000 - Longest Paths
- Longest Paths uva
- uva 10000 - Longest Paths(bellman_ford算法过)
- UVa:10000 Longest Paths (DAG上的最长路)
- UVA 10000 Longest Paths 最简单的最长路
- 10000-Longest Paths
- UVa 10000 Longest Paths (单源最长路 - floyd or 拓扑排序)
- UVA 12295 Optimal Symmetric Paths (spfa + 记忆化搜索)
- UVA 12295 Optimal Symmetric Paths(spfa+记忆化)
- uva 11823 - Two Longest Paths (最小费用流)
- uva10000 Longest Paths
- UVa10000_Longest Paths(最短路SPFA)
- FontManager 给某个页面或某些控件更改字体
- NSTimer使用机及其注意事项(一)
- Java通过百度地图API获取地址经纬度
- 【iOS开发】推送跳转处理&&UIApplicationLaunchOptionsRemoteNotificationKey
- Spark Streaming Kafka CreateDirectDStreaming 遇见的问题
- UVA 10000 Longest Paths(SPFA)
- Android自定义控件之柱型图表控件
- win10 uwp 存放网络图片到本地
- ubuntu14.04 编译源码安装PCL1.8
- JavaScript_基础_DOM
- 数据结构概念
- 最短路径问题
- 【吐血推荐】在线学习好去处
- SDL GUID 中文译本