POJ3169差分约束系统
题意:有n头牛,编号为1到n,对于关系好的ml头牛,al和bl之间的距离不大于dl,关系差的md头牛,ad和bd之间的距离不大于dd,求第1头牛和第n头牛之间的距离
分析:这是一道差分约束系统的题目,先来看一下查分约束系统的资料
http://www.cnblogs.com/void/archive/2011/08/26/2153928.html
于是上述问题可以转换成最短路问题来求解,设d[i]为第1个顶点到第i个顶点的最短距离,d[al[i]]+dl[i]>=d[bl[i]],于是d[bl[i]]-d[al[i]]<=dl[i],同理可得d[ad[i]]-d[bd[i]]<=-dd[i],然后建立最短路来求解即可,因为存在负圈,所以用Bellman-Ford来求最短路
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <vector>
#include <algorithm>
#include <set>
#include <map>
#include <bitset>
#include <cmath>
#include <queue>
#include <stack>
using namespace std;
const int maxn=;
const int INF=<<;
int al[maxn],bl[maxn],dl[maxn]; //互相之间喜欢的牛
int ad[maxn],bd[maxn],dd[maxn]; //互相之间讨厌的牛
int n,ml,md;
int d[maxn]; //最短路 void solve()
{
fill(d,d+n+,INF);
d[]=;
for(int k=;k<n;k++)
{
for(int i=;i<n;i++)
{
if(d[i+]<INF)
d[i]=min(d[i+],d[i]);
}
for(int i=;i<ml;i++)
{
if(d[al[i]]<INF)
d[bl[i]]=min(d[bl[i]],d[al[i]]+dl[i]);
}
for(int i=;i<md;i++)
{
if(d[bd[i]]<INF)
d[ad[i]]=min(d[bd[i]]-dd[i],d[ad[i]]);
}
}
} int main()
{
while(cin>>n>>ml>>md)
{
for(int i=;i<ml;i++){
scanf("%d%d%d",&al[i],&bl[i],&dl[i]);
}
for(int i=;i<md;i++){
scanf("%d%d%d",&ad[i],&bd[i],&dd[i]);
}
solve();
int res=d[n];
if(d[]<){
cout<<"-1"<<endl;
}else if(res==INF){
cout<<"-2"<<endl;
}else
cout<<res<<endl;
}
return ;
}
POJ3169差分约束系统的更多相关文章
- poj3169 差分约束系统
题意: 从1到n,n个数,从左向右依次排列. 给定两种形式的约束条件: 1.xi与yi的最大距离为dk 2.xi与yi的最小距离为dk 问满足这些限定条件的情况下,数1和n的最大距离是多少?(若约束条 ...
- 【图论】POJ-3169 差分约束系统
一.题目 Description Like everyone else, cows like to stand close to their friends when queuing for feed ...
- UVA11478 Halum [差分约束系统]
https://vjudge.net/problem/UVA-11478 给定一个有向图,每条边都有一个权值.每次你可以选择一个结点v和一个整数d,把所有以v为终点的边的权值减小d,把所有以v为起点的 ...
- BZOJ 2330: [SCOI2011]糖果 [差分约束系统] 【学习笔记】
2330: [SCOI2011]糖果 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 5395 Solved: 1750[Submit][Status ...
- ACM/ICPC 之 差分约束系统两道(ZOJ2770-POJ1201)
当对问题建立数学模型后,发现其是一个差分方程组,那么问题可以转换为最短路问题,一下分别选用Bellmanford-SPFA解题 ZOJ2770-Burn the Linked Camp //差分约束方 ...
- POJ1201 Intervals(差分约束系统)
与ZOJ2770一个建模方式,前缀和当作点. 对于每个区间[a,b]有这么个条件,Sa-Sb-1>=c,然后我就那样连边WA了好几次. 后来偷看数据才想到这题还有两个隐藏的约束条件. 这题前缀和 ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- Burn the Linked Camp(bellman 差分约束系统)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- zoj 2770 Burn the Linked Camp (差分约束系统)
// 差分约束系统// 火烧连营 // n个点 m条边 每天边约束i到j这些军营的人数 n个兵营都有容量// Si表示前i个军营的总数 那么 1.Si-S(i-1)<=C[i] 这里 建边(i- ...
随机推荐
- execlp("ls","flw","-?",(char *)0) 为什么少了最后的一个参数就不行?
execlp("ls","flw","-?",(char *)0) 为什么少了最后的一个参数就不行?
- FTP: Configuring server users..
4 points to create a user to uploade to ftproot.. this user must be an administrator, and be able to ...
- Swift 学习笔记 (一)
原创: 转载请注明出处 Extention try catch rxSwift internal public private var let as as? 强转 ? ! didSe ...
- PHP DES加密
public function encrypt($str) { //$str = "hh"; $key = "key:123";//密钥 $cipher = M ...
- 完整版getByClass2016/4/20
function getByclass(parent,sclass) { var re=new RegExp('\\b'+sclass+'\\b','i') var aEli=parent.getEl ...
- POJ 2186 Popular Cows tarjan缩点算法
题意:给出一个有向图代表牛和牛喜欢的关系,且喜欢关系具有传递性,求出能被所有牛喜欢的牛的总数(除了它自己以外的牛,或者它很自恋). 思路:这个的难处在于这是一个有环的图,对此我们可以使用tarjan算 ...
- Node.js学习 - RESTFul API
REST Representational State Transfer (表述性状态转移), 是一组架构约束条件和原则.满足这些约束条件和原则的应用程序或设计就是RESTful. RESTful W ...
- w3chtml页面和css书写规范
http://www.cnblogs.com/Wenwang/archive/2011/09/07/2169881.html
- swift(2)元祖(Tuple)
let somePoint = (, ) switch somePoint { , ): // 位于远点 println("(0, 0) is at the origin") ): ...
- CodeForces 678D Iterated Linear Function
简单矩阵快速幂. #include<cstdio> #include<cstring> #include<cmath> #include<algorithm& ...