POJ 3169 Layout ---差分约束 + 最短路 + 含负边处理
2016-07-04 20:59
357 查看
题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离
>= 0。这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w。2.有md组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w。问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛
的距离可以无限远,则输出-2,否则则输出牛[1]和牛
之间的最大距离。
分析:采用SPFA算法求最短路+差分约束
样例:
Sample Input
Sample Output
>= 0。这些牛的距离存在着一些约束关系:1.有ml组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 <= w。2.有md组(u, v, w)的约束关系,表示牛[u]和牛[v]之间的距离必须 >= w。问如果这n头无法排成队伍,则输出-1,如果牛[1]和牛
的距离可以无限远,则输出-2,否则则输出牛[1]和牛
之间的最大距离。
分析:采用SPFA算法求最短路+差分约束
样例:
Sample Input
4 2 1 1 3 10 2 4 20 2 3 3
Sample Output
27
#include <iostream> #include <cstdio> #include <cstdlib> #include <queue> #include <cstring> #include <string> #define INF 0x3f3f3f3f using namespace std; const int MAXN = 1005; const int E = 20005; int n,ml,md; int dist[MAXN],cnt[MAXN],p[E],h[MAXN],nxt[E],cost[E]; bool vis[MAXN]; int e; void add(int u,int v,int w) { p[e] = v; cost[e] = w; nxt[e] = h[u]; h[u] = e++; } void init() { e=0; int u,v,w;int i; cin >> n>> ml >> md; memset(h,-1,sizeof(h)); for(i=1;i<=n;i++) { dist[i] = INF; } for(i=1;i<=ml;i++) { cin >> u >> v >> w; add(u,v,w); } for(i=1;i<=md;i++) { cin >> u >> v >> w; add(v,u,-w); } for(int i = 2;i<=n;i++)//di <= di+1 + 0 { add(i,i-1,0); } } bool relax(int u,int v,int w) { if(dist[v]>dist[u]+w) { dist[v] = dist[u]+w; return true; } return false; } int SPFA(int src,int n) { memset(vis,false,sizeof(vis)); memset(cnt,0,sizeof(cnt)); dist[src] = 0; queue<int> Q; Q.push(src); vis[src] = true; ++cnt[src]; int i=0; while(!Q.empty()) { int u,v; u = Q.front(); Q.pop(); vis[u] = false; for(i=h[u];i!=-1;i=nxt[i]) { v=p[i]; if(relax(u,v,cost[i])&&!vis[v]) { Q.push(v); vis[v] = true; if(++cnt[v]>=n) return -1; } } } if(dist == INF) return -2; return dist ; } int main() { init(); cout<< SPFA(1,n)<< endl; return 0; }
相关文章推荐
- 双GPU-c++MATLAB混合编程
- ubuntu16上安装ROS Kinetic
- java原子操作类-原子数组类
- Project第三天 实现百度定位后计算距离和进行排序
- C# 对象的使用
- 增强 nginx 的 SSL 安全性
- PAT (Advanced Level) 1094. The Largest Generation (25)
- WPF实现控件拖动
- Android Studio快捷键整理
- Android相对布局示例
- Cassandra删除数据的坑
- 动态规划--随笔(个人理解)
- 表达式求值
- block开辟内存(五)
- 内部类里的局部内部类
- Swift与Java语法结构对比
- PHP自动化测试(一)make test 和 phpt
- Spring配置文件的命名空间URI
- WPF ListView和ListBox等双击事件问题
- Spring(一):Spring概念、基本例子入门