题目链接:http://poj.org/problem?id=3169

很好的差分约束入门题目,自己刚看时学呢

代码:

 #include<iostream>
#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<queue>
using namespace std;
#define INF 0x3f3f3f3f
#define maxn 1010
int dis[maxn];
int path[maxn];
int inq[maxn];
int cnt[maxn];
class node
{
public:
int to;
int w;
int next;
};
node edge[maxn*];
int head[maxn*];
int tol;
int n,ML,MD;
void add(int u,int v,int w)
{
edge[tol].to=v;
edge[tol].w =w;
edge[tol].next=head[u];
head[u]=tol++;
};
queue<int>Q;
bool SPFA()
{
memset(inq,,sizeof(inq));
memset(cnt,,sizeof(cnt));
int v0=;
for(int i=;i<=n;i++)
{
dis[i]=INF;
path[i]=v0;
inq[i]=;
}
dis[v0]=;
path[v0]=v0;
inq[v0]++;
cnt[v0]++;
Q.push(v0);
while(!Q.empty())
{
int u=Q.front();
Q.pop();
inq[u]--;
int tmp=head[u]; while(tmp!=-)
{
int v=edge[tmp].to;
int w=edge[tmp].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
path[v]=u;
if(inq[v]==)
{
inq[v]++;
if(++cnt[v]>n) return false;
Q.push(v); }
}
tmp=edge[tmp].next;
}
}
return true;
}
int main()
{
int a,b,d;
while(scanf("%d%d%d",&n,&ML,&MD)!=EOF)
{
tol=;
memset(head,-,sizeof(head));
while(ML--)
{
scanf("%d%d%d",&a,&b,&d);
add(a,b,d); }
while(MD--)
{
scanf("%d%d%d",&a,&b,&d);
add(b,a,-d);
}
if(!SPFA()) cout<<"-1"<<endl;
else
if(dis[n]>=INF) cout<<"-2"<<endl;
else cout<<dis[n]<<endl; }
return ;
}

poj Layout 差分约束+SPFA的更多相关文章

  1. POJ-3169 Layout (差分约束+SPFA)

    POJ-3169 Layout:http://poj.org/problem?id=3169 参考:https://blog.csdn.net/islittlehappy/article/detail ...

  2. POJ 1201 差分约束+SPFA

    思路: 差分约束,难在建图.(我是不会告诉你我刚学会SPFA的...) 把每个区间的ai–>bi连一条长度为ci的边. k–>k+1连一条长度为0的边. k+1–>k连一条长度为-1 ...

  3. O - Layout(差分约束 + spfa)

    O - Layout(差分约束 + spfa) Like everyone else, cows like to stand close to their friends when queuing f ...

  4. 【poj3169】【差分约束+spfa】

    题目链接http://poj.org/problem?id=3169 题目大意: 一些牛按序号排成一条直线. 有两种要求,A和B距离不得超过X,还有一种是C和D距离不得少于Y,问可能的最大距离.如果没 ...

  5. POJ 3169 Layout(差分约束+链式前向星+SPFA)

    描述 Like everyone else, cows like to stand close to their friends when queuing for feed. FJ has N (2 ...

  6. poj 3169 Layout(差分约束+spfa)

    题目链接:http://poj.org/problem?id=3169 题意:n头牛编号为1到n,按照编号的顺序排成一列,每两头牛的之间的距离 >= 0.这些牛的距离存在着一些约束关系:1.有m ...

  7. (简单) POJ 3169 Layout,差分约束+SPFA。

    Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...

  8. POJ 1364 / HDU 3666 【差分约束-SPFA】

    POJ 1364 题解:最短路式子:d[v]<=d[u]+w 式子1:sum[a+b+1]−sum[a]>c      —      sum[a]<=sum[a+b+1]−c−1  ...

  9. POJ 3159 Candies(差分约束+spfa+链式前向星)

    题目链接:http://poj.org/problem?id=3159 题目大意:给n个人派糖果,给出m组数据,每组数据包含A,B,C三个数,意思是A的糖果数比B少的个数不多于C,即B的糖果数 - A ...

随机推荐

  1. pip --upgrade批量更新过期的python库

    pip --upgrade批量更新过期的python库 python pip 转载请标明出处:marsggbo网易博客http://blog.163.com/hexin_mars_blog/blog/ ...

  2. HTMLElement

    参考文档:MDN HTMLElement 一.继承关系 所有HTML元素都是由HTMLElement或者其更具体的子类型来表示的. HTMLElement继承自Element,并实现了GlobalEv ...

  3. Linux之例行(任务调度)

    一. 例行命令之at 1.1 at 仅执行一次就从Linux任务中取消  1.2 at 指令可以将工作命令写入工作记录文件,工作记录文件默认存放在/var/spool/at目录内  1.3 at 工作 ...

  4. VB中的GDI编程-1 设备环境DC

    p{ font-size: 15px; } .alexrootdiv>div{ background: #eeeeee; border: 1px solid #aaa; width: 99%; ...

  5. Windows下Nginx的安装与使用(一):配置端口转发

    什么是端口转发 当我们在服务器上搭建一个图书以及一个电影的应用,其中图书应用启动了 8001 端口,电影应用启动了 8002 端口.此时如果我们可以通过: localhost:8001 //图书 lo ...

  6. 第一个python爬虫程序

    1.安装Python环境 官网https://www.python.org/下载与操作系统匹配的安装程序,安装并配置环境变量 2.IntelliJ Idea安装Python插件 我用的idea,在工具 ...

  7. 在ASP.NET MVC4中配置Castle

    ---恢复内容开始--- Castle是针对.NET平台的一个非常优秀的开源项目,重点是开源的哦.它在NHibernate的基础上进一步封装,其原理基本与NHibernate相同,但它较好地解决NHi ...

  8. web前端概念巩固(一)

    h5: 1.web语义化 Web语义化是指在进行HTML结构.表现.行为设计时,尽量使用语义化的标签,使程序代码简介明了,易于进行Web操作和网站SEO,方便团队协作的一种标准,以图实现一种" ...

  9. Spring+Redis(keyspace notification)实现定时任务(订单过期自动关闭)

    1.起因 最近公司项目要做订单超期未支付需自动关闭,首先想到的是用spring的定时器(@Schedule),结果领导举各种例子说会影响性能,只能作罢.后来想能不能基于redis实现, 学习(baid ...

  10. 栈实现getMin

    题目 实现一个特殊的栈,在实现栈的基本功能的基础上,在实现返回栈中最小元素的操作. 要求 pop.push.getMin操作的时间复杂度都是O(1). 设计的栈类型可以使用现成的栈结构. 解答 在设计 ...