JZOI1142 排队布局
#include <bits/stdc++.h>
using namespace std;
inline int read() {
int x = 0,tmp = 1;char ch = getchar();
while( ch < '0' || ch > '9' ) {if ( ch == '-' ) tmp = -1; ch = getchar();}
while( ch >= '0' && ch <= '9'){x = x * 10 + ch - '0'; ch = getchar(); }
return x * tmp;
}
int INF;
struct Point {
int to, next, w;
} edge[410000];
int head[1100], idx = 0, dis[1100], in[1100];
bool vis[1100];
inline void ade( int u, int v, int w ) {
edge[++ idx].to = v;
edge[idx].w = w;
edge[idx].next = head[u];
head[u] = idx;
}
int spfa( int _sta, int _end ) {
memset( in, 0, sizeof( in ) );
memset( vis, 0, sizeof( vis ) );
memset( dis, 127, sizeof( dis ) ); INF = dis[0];
vis[_sta] = 1; dis[_sta] = 0;
queue< int > Q;
Q.push( _sta ); in[_sta] = 1;
while( !Q.empty() ) {
int now = Q.front();
Q.pop();
vis[now] = 0;
for( int i = head[now] ; i != -1 ; i = edge[i].next ) {
int son = edge[i].to, w = edge[i].w;
if( dis[son] > dis[now] + w ) {
dis[son] = dis[now] + w;
if( !vis[son] ) {
in[son] ++;
if( in[son] > _end ) return -1;
vis[son] = 1;
Q.push( son );
}
}
}
}
if( dis[_end] == INF ) return -2;
return dis[_end];
}
int main() {
memset( head, -1, sizeof( head ) );
int N = read(), M1 = read(), M2 = read();
for( int i = 1 ; i <= M1 ; ++ i ) {
int u = read(), v = read(), w = read();
ade( u, v, w );
}
for( int i = 1 ; i <= M2 ; ++ i ) {
int u = read(), v = read(), w = read();
ade( v, u, -w );
}
for( int i = 2 ; i <= N ; ++ i ) ade( i, i - 1, 0 );
printf( "%d\n", spfa( 1, N ) );
return 0;
}
/**************************************************************
Problem: 1142
User: ARZhu
Language: C++
Result: 正确
Time:4 ms
Memory:6364 kb
****************************************************************/
JZOI1142 排队布局的更多相关文章
- 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束
[BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...
- 【bzoj1731】Layout 排队布局
1731: [Usaco2005 dec]Layout 排队布局 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 868 Solved: 495[Subm ...
- 1731: [Usaco2005 dec]Layout 排队布局*
1731: [Usaco2005 dec]Layout 排队布局 题意: n头奶牛在数轴上,不同奶牛可以在同个位置处,编号小的奶牛必须在前面.m条关系,一种是两头奶牛距离必须超过d,一种是两头奶牛距离 ...
- 排队(BZOJ1731:[Usaco2005 dec]Layout 排队布局)
[问题描述] Czy喜欢将他的妹子们排成一队.假设他拥有N只妹纸,编号为1至N.Czy让他们站成一行,等待自己来派送营养餐.这些妹纸按照编号大小排列,并且由于它们都很想早点吃饭,于是就很可能出现多只妹 ...
- [bzoj1731] [Usaco2005 dec]Layout 排队布局
差分约束系统...因为题目要求的是1和n的最大距离所以这题就跑最长路.. 对于互相反感的牛(i与j互相反感,彼此距离至少为len,i<j),就有dis[j]-dis[i]>=len.就加一 ...
- [Usaco2005 dec]Layout 排队布局 差分约束
填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...
- BZOJ1731:[USACO]Layout 排队布局(差分约束)
Description Like everyone else, cows like to stand close to their friends when queuing for feed. FJ ...
- bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束
Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相 ...
- 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局
[题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...
随机推荐
- linux系统的文件保护
一些文件在Linux下看上去可能一切正常,但当您尝试删除的时候,居然也会报错,就像下边一样: [root@linux236 root]# ls -l 1.txt-rw-r--r-- 1 root ro ...
- 2017 NOIp 初赛体验
很菜...我还是太蒟蒻了. d 老师太强了... 应该能有七十几分 初赛稳了 Update: 五十几分...
- Linux v4l2编程(摄像头信息采集)
基于Linux3.4.2,自己做一点儿视频信息采集及网络传输的小实验,边做边学,一些基础知识同步整理..... 1. 定义 V4L2(Video For Linux Two) 是内核提供给应用程序访问 ...
- Missing artifact net.sf.json-lib:json-lib:jar:2.4
Missing artifact net.sf.json-lib:json-lib:jar:2.4 出现上述这种错误就是JAR没有引入进来 这时候发现是因为JDK版本的问题,所以需要在加一句 < ...
- (转)spring ioc原理(看完后大家可以自己写一个spring)
原文地址:https://blog.csdn.net/it_man/article/details/4402245 最近,买了本Spring入门书:spring In Action .大致浏览了下感觉 ...
- JAVA中一个汉字占多少个字符(转载)
1.先说重点: 不同的编码格式占字节数是不同的,UTF-8编码下一个中文所占字节也是不确定的,可能是2个.3个.4个字节: 2.以下是源码: 1 @Test 2 public void test1() ...
- 运维01 VMware与Centos系统安装
VMware与Centos系统安装 今日任务 1.Linux发行版的选择 2.vmware创建一个虚拟机(centos) 3.安装配置centos7 4.xshell配置连接虚拟机(centos) ...
- upc组队赛5 Assembly Required【思维】
Assembly Required 题目描述 Princess Lucy broke her old reading lamp, and needs a new one. The castle ord ...
- C++基础知识随记
一.什么情况必须使用初始化列表的方式声明构造函数? 1.包含常量类型的成员 2.包含引用类型的成员 3.包含没有默认构造函数的类类型成员 4.优点:对于包含有类类型成员的类来说,省去了调用一次默认构造 ...
- 【模板】fread读入优化 & fwrite输出优化
#include <iostream> #include <cstdio> #include <cctype> #define SIZE (1 << 2 ...