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. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...
随机推荐
- Cisco基础(五):配置静态NAT、配置端口映射、配置动态NAT、PAT配置、办公区Internet的访问
一.配置静态NAT 目标: 随着接入Internet的计算机数量的不断猛增,IP地址资源也就愈加显得捉襟见肘.事实上,除了中国教育和科研计算机网(CERNET)外,一般用户几乎申请不到整段的C类IP地 ...
- mapreduce求共同好友
逻辑分析 以下是qq的好友列表数据,冒号前是一个用户,冒号后是该用户的所有好友(数据中的好友关系是单向的) A:B,C,D,F,E,O B:A,C,E,K C:F,A,D,I D:A,E,F,L E: ...
- AcWing 209. 装备购买 (高斯消元线性空间)打卡
脸哥最近在玩一款神奇的游戏,这个游戏里有 n 件装备,每件装备有 m 个属性,用向量z[i]=(ai,1,ai,2,..,ai,m)z[i]=(ai,1,ai,2,..,ai,m) 表示,每个装备需要 ...
- VMware Hyper-V不兼容
VMware Workstation Windows系統的Hyper-V不相容 禁用Device Guard或Credential Guard 1. 以管理員身份運行Windows Powershel ...
- echarts 柱状图 X(Y)轴数据过多时,滑动以及内置缩放的问题
前言:在开发中碰到的情况(菜鸟出门). 在使用echarts 图表的时候发现要展示的数据过多,但是系统留的展示框太小,造成数据都挤压在一块(不好看而且新感觉很不专业). ...
- PHP面试 javascript和jQuery 基础
JavaScript基础 JavaScript语法 变量的定义:变量必须以字母开头 可以使用$和 _ 符号开头 变量名称对大小写敏感 使用 var 关键词声明变量 可以一次声明多个变量 ...
- The document cannot be opened. It has been renamed, deleted or moved.
In the Individual components section of the Visual Studio Installer, make sure that Razor Language S ...
- python学习笔记:python操作redis
Redis 是一个高性能的key-value数据库.它支持存储的value类型包括string(字符串).list(链表).set(集合).zset(sorted set --有序集合)和hash(哈 ...
- Axon 3.0.x 框架简介官方文档
因为需要用到,但是在网上对应的资料实在是很少,只有迎着头皮看官网文档并配合翻译器.如有误导多多包涵. Axon 什么是 Axon Axon Framework 通过支持开发人员应用命令查询责任隔离(C ...
- 转 python3 读取 ini配置文件
在代码中经常会通过ini文件来配置一些常修改的配置.下面通过一个实例来看下如何写入.读取ini配置文件. 需要的配置文件是: 1 [path] 2 back_dir = /Users/abc/Pych ...