#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 排队布局的更多相关文章

  1. 【BZOJ1731】[Usaco2005 dec]Layout 排队布局 差分约束

    [BZOJ1731][Usaco2005 dec]Layout 排队布局 Description Like everyone else, cows like to stand close to the ...

  2. 【bzoj1731】Layout 排队布局

    1731: [Usaco2005 dec]Layout 排队布局 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 868  Solved: 495[Subm ...

  3. 1731: [Usaco2005 dec]Layout 排队布局*

    1731: [Usaco2005 dec]Layout 排队布局 题意: n头奶牛在数轴上,不同奶牛可以在同个位置处,编号小的奶牛必须在前面.m条关系,一种是两头奶牛距离必须超过d,一种是两头奶牛距离 ...

  4. 排队(BZOJ1731:[Usaco2005 dec]Layout 排队布局)

    [问题描述] Czy喜欢将他的妹子们排成一队.假设他拥有N只妹纸,编号为1至N.Czy让他们站成一行,等待自己来派送营养餐.这些妹纸按照编号大小排列,并且由于它们都很想早点吃饭,于是就很可能出现多只妹 ...

  5. [bzoj1731] [Usaco2005 dec]Layout 排队布局

    差分约束系统...因为题目要求的是1和n的最大距离所以这题就跑最长路.. 对于互相反感的牛(i与j互相反感,彼此距离至少为len,i<j),就有dis[j]-dis[i]>=len.就加一 ...

  6. [Usaco2005 dec]Layout 排队布局 差分约束

    填坑- 差分约束一般是搞一个不等式组,求xn-x1的最大最小值什么的,求最大值就转化成xa<=xb+w这样的,然后建图跑最短路(这才是最终约束的),举个例子 x1<=x0+2x2<= ...

  7. BZOJ1731:[USACO]Layout 排队布局(差分约束)

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

  8. bzoj 1731: [Usaco2005 dec]Layout 排队布局 ——差分约束

    Description 当排队等候喂食时,奶牛喜欢和它们的朋友站得靠近些.FJ有N(2<=N<=1000)头奶牛,编号从1到N,沿一条直线站着等候喂食.奶牛排在队伍中的顺序和它们的编号是相 ...

  9. 【BZOJ】1731: [Usaco2005 dec]Layout 排队布局

    [题意]给定按编号顺序站成一排的牛,给定一些约束条件如两牛距离不小于或不大于某个值,求1和n的最大距离.无解输出-1,无穷解输出-2. [算法]差分约束+最短路 [题解]图中有三个约束条件,依次分析: ...

随机推荐

  1. apue 第10章 信号signal

    每种信号都有名字,都是以SIG开头 信号机制最简单的接口是signal函数 #include <signal.h> typedef void (*sighandler_t)(int); s ...

  2. hdu 6134 Battlestation Operational (莫比乌斯反演+埃式筛)

    Problem Description   > The Death Star, known officially as the DS-1 Orbital Battle Station, also ...

  3. eclipse debug (调试)基础

    进入debug模式: 1.设置断点 2.启动servers端的debug模式 3.运行程序,在后台遇到断点时,进入debug调试状态 ============================= 作用域 ...

  4. LiveTelecast直播平台技术图谱skill-map

    #直播平台技术图谱 ----##直播 ----###采集- **iOS** * HTTP Live Streaming * DirectShow- **Android** * setPreviewCa ...

  5. 69、schema的相关方法

    public class SObjectSchema { public void testSchema(){ //获取SObject的token //1.先获取所有token,然后通过key获取需要的 ...

  6. IDEA 报错记录

    IDEA 报错记录 Process finished with exit code 0 这种主要是配了默认的 Tomcat ,然后又配置了外部的 Tomcat.解决办法,注释掉默认的: <dep ...

  7. ubuntu 18.04 自启动

    按下面二种方式打开自启动设置窗口,设置启动参数:(两种方式) 方式一:在桌面左上角的搜索框中输入Startup Applications,打开,点击Add,Name处填open_terminal(自定 ...

  8. windows下Docker安装MySQL

    # docker 中下载 mysql docker pull mysql #启动 docker run --name mysql -p 3306:3306 -e MYSQL_ROOT_PASSWORD ...

  9. python学习笔记:模块——time模块

    timetime模块提供各种时间相关的功能,与时间相关的模块有:time,datetime,calendar等. 时间有三种表示方式,一种是时间戳.一种是格式化时间.一种是时间元组.时间戳和格式化时间 ...

  10. gitlab搭建使用

    1.安装gitlab __环境准备__ [root@gitlab ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) [r ...