#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. PHP chgrp() 函数

    定义和用法 chgrp() 函数改变指定文件的用户组. 如果成功则返回 TRUE,如果失败则返回 FALSE. 语法 chgrp(file,group) 参数 描述 file 必需.规定要检查的文件. ...

  2. 重新学习MySQL数据库11:以Java的视角来聊聊SQL注入

    本文转自互联网 本系列文章将整理到我在GitHub上的<Java面试指南>仓库,更多精彩内容请到我的仓库里查看 https://github.com/h2pl/Java-Tutorial ...

  3. win10配置 samba

    一.先確認Linux中smb正確配置可以使用命令smbclient -L //localhost/ 二.win10配置1.打開win10對smb1.0/cifs檔共用支援2.本地群組原則編輯,修改如下 ...

  4. (转)使用OpenGL ES显示图像

    编写:jdneo - 原文:http://developer.android.com/training/graphics/opengl/index.html 转:http://hukai.me/and ...

  5. scrapy原理

    scarpy据说是目前最强大的爬虫框架,没有之一.就是这么自信. 官网都是这么说的. An open source and collaborative framework for extracting ...

  6. 优化问题及KKT条件

    整理自其他优秀博文及自己理解. 目录 无约束优化 等式约束 不等式约束(KKT条件) 1.无约束优化 无约束优化问题即高数下册中的 “多元函数的极值"  部分. 驻点:所有偏导数皆为0的点: ...

  7. CodeForces - 841B-Godsend-思维

    Leha somehow found an array consisting of n integers. Looking at it, he came up with a task. Two pla ...

  8. Spring Boot控制上传文件大小

    spring: http: multipart: max-file-size: 5MB max-request-size: 20MB

  9. 跨域篇--JSONP原理

    一篇文章让你明白 jsonp原理详解 什么是JSONP? 先说说JSONP是怎么产生的: 其实网上关于JSONP的讲解有很多,但却千篇一律,而且云里雾里,对于很多刚接触的人来讲理解起来有些困难,着用自 ...

  10. Json解析之FastJson

    版权声明:转载请注明出处 https://blog.csdn.net/heqiangflytosky/article/details/37659943 1.FastJson介绍 FastJson是阿里 ...