codevs 1242 布局(查分约束+SPFA)
/*
查分约束.
给出的约束既有>= 又有<= 这时统一化成一种
Sb-Sa>=x 建边 a到b 权值为x
Sb-Sa<=y => Sa-Sb>=-y 建边 b到a 权值为-y
然后跑最短路 SPFA 判断到不了终点 判断负环的死循环.
*/
#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
#define maxn 20010
using namespace std;
int n,m1,m2,num,head[maxn],dis[maxn],f[maxn],use[maxn],falg;
struct node
{
int v,t,pre;
}e[maxn];
int init()
{
int x=;char s;s=getchar();
while(s<''||s>'')s=getchar();
while(s>=''&&s<=''){x=x*+s-'';s=getchar();}
return x;
}
void Add(int from,int to,int dis)
{
num++;
e[num].v=to;
e[num].t=dis;
e[num].pre=head[from];
head[from]=num;
}
void SPFA()
{
queue<int>q;
q.push();
f[]=;
dis[]=;
use[]++;
while(!q.empty())
{
int k=q.front();
q.pop();
f[k]=;
if(use[k]>n)
{
falg=;
break;
}
for(int i=head[k];i;i=e[i].pre)
if(dis[e[i].v]>dis[k]+e[i].t)
{
dis[e[i].v]=dis[k]+e[i].t;
if(f[e[i].v]==)
{
q.push(e[i].v);
f[e[i].v]=;
use[e[i].v]=use[k]+;
}
}
}
}
int main()
{
n=init();m1=init();m2=init();
int x,y,z;
for(int i=;i<=m1;i++)
{
x=init();y=init();z=init();
Add(x,y,z);
}
for(int i=;i<=m2;i++)
{
x=init();y=init();z=init();
Add(y,x,-z);
}
memset(dis,,sizeof(dis));
SPFA();
if(dis[n]>)printf("-2");
else if(falg==)printf("-1");
else printf("%d\n",dis[n]);
return ;
}
codevs 1242 布局(查分约束+SPFA)的更多相关文章
- 洛谷P1993 小 K 的农场(查分约束)
/* 加深一下对查分约束的理解 建图的时候为了保证所有点联通 虚拟一个点 它与所有点相连 权值为0 然后跑SPFA判负环 这题好像要写dfs的SPFA 要不超时 比较懒 改了改重复进队的条件~ */ ...
- poj 1201 Interval (查分约束)
/* 数组开大保平安. 查分约束: 输入的时候维护st和end 设每个点取元素di个 维护元素个数前缀和s Sbi-Sai-1>=ci 即:建立一条从ai-1到bi的边 权值为ci 表示ai到b ...
- Codevs 1242 布局 2005年USACO(差分约束)
1242 布局 2005年USACO 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近 ...
- BZOJ2330 糖果题解 查分约束
BZOJ 2330 糖果题解 差分约束系统 + SPFA 题目传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=2330 Description ...
- POJ1364基本的查分约束问题
题意: 给了由n个数组成的一个数列,然后给你各种区间的和是大于ci还是小于ci啥的,最后问你是否冲突. 思路: 差分约束水题,不过wa了两次,原因处理区间问题的细节马虎了,说下 ...
- POJ1094查分约束,判断关系是否唯一
题意: 给你一些a<b的关系,然后有三组询问. 1 当前这组之后如果能确定这n个数的大小关系,那么就输出关系 2 当前时候出现bug,就是和前面如果冲突,那么就不行 3 最后的答案是 ...
- zoj Burn the Linked Camp (查分约束)
Burn the Linked Camp Time Limit: 2 Seconds Memory Limit: 65536 KB It is well known that, in the ...
- hdu 1384 查分约束
#include<stdio.h> /* 要善于挖掘隐含条件 dis[v]-dis[u]>=bian[i].w;一个条件(u,v,bian[i].w); dis[i+1]>=d ...
- Integer Intervals POJ - 1716_查分约束_
Code: #include<cstdio> #include<queue> #include<algorithm> using namespace std; co ...
随机推荐
- IPython学习笔记
IPython 前言 Life is short, you need Python 最近开始学习数据挖掘,需要使用到Python,其实大学时代就有接触过Python,但是却始终没有系统的进行学习过. ...
- Mvc 页面缓存 OutputCache VaryByCustom
优化网站,dotNet MVC 可以通过(OutputCache)特性在某些Action上使用缓存,如果我们想要自定义缓存依据可以通过如下方式进行: 第一步, 在 global.asax.cs 文件中 ...
- UnixODBC
UnixODBC下载安装地址:http://www.unixodbc.org/ DOWNLOAD Distribution Format unixODBC is currently availible ...
- 缩小jquery体积
jQuery 分析 据统计,目前全世界57.3%的网站使用它.也就是说,10个网站里面,有6个使用jQuery.如果只考察使用工具库的网站,这个比例就会上升到惊人的91.7%. 虽然jQuery如此受 ...
- Inna and Binary Logic
Codeforces Round #234 (Div. 2) E:http://codeforces.com/problemset/problem/400/E 题意:给你n个数,然后每相邻的两个数可以 ...
- Mondriaan's Dream
poj2411:http://poj.org/problem?id=2411 题意:给你1*2的方块,让你把n*m的房间填好有多少种方式. 题解:状压dp.这一题,我是不会做了,看懂了题解之后,震惊了 ...
- UIAutomation识别UI元素
MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词 ...
- npm check failed 解决办法
npm ERR! shasum check failed for C:\Users\MM\AppData\Local\Temp\npm-10900-415697c8\registry.npmjs.or ...
- 一段JAVA签名算法的PHP改写
源代码是这样的: public class AuthorizationSignature { public static String createSignature(String verb, Str ...
- Unity 用C#脚本读取JSON文件数据
读取JSON文件数据网上有很多方法吗,这里采用SimpleJSON,关于SimpleJSON的介绍参考以下链接:http://wiki.unity3d.com/index.php/SimpleJSON ...