#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. delphi文件类型

    1.DPR: Delphi Project文件,包含了Pascal代码.应用系统的工程文件2.PAS: Pascal文件,Pascal单元的源代码,可以是与窗体有关的单元或是独立的单元.3.DFM:D ...

  2. MCS-51系列单片机和MCS-52系列单片机有何异同

    MSC-51:1,片内4K字节程序存储器:2,片内128字节数据存储器:3,片内2个16位硬件定时器/计数器.MSC-52: 1,片内8K字节程序存储器:2,片内256字节数据存储器:3,片内3个16 ...

  3. Use Git Credential Managers to Authenticate to Azure Repos

    https://docs.microsoft.com/en-us/azure/devops/repos/git/set-up-credential-managers?view=azure-devops ...

  4. 如何用python读写CSV 格式文件

    工作中经常会碰到读写CSV文件的情况.记录下,方便自己以后查询并与大家一起分享: 写CSV文件方法一: import csv          #导入CSV with open("D:\eg ...

  5. ubuntu php多版本共存切换

    做开发时,由于本机开发的php版本跟线上发布的php版本不一致,很容易在上线后,发现因版本的影响导致一些bug,但又不想重新去换本机的php版本,那么多版本共存就很方便了!有必要时,切换到指定版本测试 ...

  6. 数学&计算机工程常用希腊字母表及其发音

  7. Python 文件及文件夹处理

    import os,shutil def getfilelist(filepath): filelist = os.listdir(filepath) # 获取filepath文件夹下的所有的文件 # ...

  8. SpringBoot2.0拦截器 与 1.X版本拦截器 的实现

    1.5  版本 先写个拦截器,跟xml配置方式一样,然后将拦截器加入spring容器管理 .接着创建 配置文件类 继承 WebMvcConfigurerAdapter 类,重写父类方法addInter ...

  9. Msys2升级后不能编译

    Msys2升级后不能编译 */--> code {color: #FF0000} pre.src {background-color: #002b36; color: #839496;} cod ...

  10. [轉]Reverse a singly linked list

    Reverse a singly linked list  http://angelonotes.blogspot.tw/2011/08/reverse-singly-linked-list.html ...