博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
洛谷P1993 小 K 的农场
阅读量:7019 次
发布时间:2019-06-28

本文共 1994 字,大约阅读时间需要 6 分钟。

 

题目描述

小 K 在 Minecraft 里面建立很多很多的农场,总共 n 个,以至于他自己都忘记了每个

农场中种植作物的具体数量了,他只记得一些含糊的信息(共 m 个),以下列三种形式描

述:

  1. 农场 a 比农场 b 至少多种植了 c 个单位的作物。

  2. 农场 a 比农场 b 至多多种植了 c 个单位的作物。

  3. 农场 a 与农场 b 种植的作物数一样多。

但是,由于小 K 的记忆有些偏差,所以他想要知道存不存在一种情况,使得农场的种

植作物数量与他记忆中的所有信息吻合。

输入输出格式

输入格式:

 

从 farm.in 中输入数据

第一行包括两个整数 n 和 m,分别表示农场数目和小 K 记忆中的信息数目。

接下来 m 行:

如果每行的第一个数是 1,接下来有 3 个整数 a,b,c,表示农场 a 比农场 b 至少多种植

了 c 个单位的作物。

如果每行的第一个数是 2,接下来有 3 个整数 a,b,c,表示农场 a 比农场 b 至多多种植

了 c 个单位的作物。如果每行的第一个数是 3,家下来有 2 个整数 a,b,表示农场 a 终止的

数量和 b 一样多。

 

输出格式:

 

输出到 farm.out 中

如果存在某种情况与小 K 的记忆吻合,输出“Yes”,否则输出“No”。

 

输入输出样例

输入样例#1:
3 33 1 21 1 3 12 2 3 2
输出样例#1:
Yes

说明

对于 100% 的数据保证:1 ≤ n,m,a,b,c ≤ 10000。

 

差分约束系统。

建好边以后跑SPFA,如果发现图中有负环,则不能满足要求。

↑SPFA判负环还是DFS方便

1 /*by SilverN*/ 2 #include
3 #include
4 #include
5 #include
6 #include
7 #include
8 using namespace std; 9 const int mxn=24010;10 int read(){11 int x=0,f=1;char ch=getchar();12 while(ch<'0' || ch>'9'){ if(ch=='-')f=-1;ch=getchar();}13 while(ch>='0' && ch<='9'){x=x*10+ch-'0';ch=getchar();}14 return x*f;15 }16 struct edge{17 int v,nxt,dis;18 }e[mxn];19 int hd[mxn],mct=0;20 void add_edge(int u,int v,int dis){21 e[++mct].v=v;e[mct].dis=dis;e[mct].nxt=hd[u];hd[u]=mct;22 return;23 }24 int n,m;25 bool inq[mxn];26 int dis[mxn];27 bool SPFA(int u){28 inq[u]=1;29 for(int i=hd[u];i;i=e[i].nxt){30 int v=e[i].v;31 if(dis[v]>dis[u]+e[i].dis){32 dis[v]=dis[u]+e[i].dis;33 if(inq[v])return 1;34 if(SPFA(v))return 1;35 }36 }37 inq[u]=0;38 return 0;39 }40 int main(){41 n=read();m=read();42 int i,j,a,b,c;43 for(i=1;i<=m;i++){44 j=read();a=read();b=read();45 if(j==1){46 c=read();47 add_edge(a,b,-c);48 }else if(j==2){49 c=read();50 add_edge(b,a,c);51 }else add_edge(a,b,0),add_edge(b,a,0);52 }53 memset(dis,0x3f,sizeof dis);54 for(i=1;i<=n;i++){55 dis[i]=0;56 if(SPFA(i)){printf("No\n");return 0;}57 }58 printf("Yes\n");59 return 0;60 }

 

转载于:https://www.cnblogs.com/SilverNebula/p/6050421.html

你可能感兴趣的文章
COALESCE操作符
查看>>
ubuntu16.04下python2、python3环境选择与python升级(pip版本切换)
查看>>
topcoder srm 435 div1
查看>>
Java读取文本指定的某一行内容的方法
查看>>
leetcode--Best Time to Buy and Sell Stock II
查看>>
第二周助教总结
查看>>
Best Time to Buy and Sell Stock II
查看>>
Could not load file or assembly 'System.Data.SqlServerCe, Version=4.0.0.0, Culture=neutral..
查看>>
php 调用 web Server(短信接口示例)
查看>>
bootstrap-table组合表头
查看>>
蓝桥杯 全球变暖(dfs)
查看>>
[UML]UML系列——类图Class
查看>>
机器学习之支持向量机(Support Vector Machine)
查看>>
模型小型化小结
查看>>
fopen()和fclose()
查看>>
虹软arcface人脸识别集成到项目中
查看>>
[c语言]运算符的优先级与结合性
查看>>
[题解]Mail.Ru Cup 2018 Round 1 - B. Appending Mex
查看>>
VMware Workstation 不可恢复错误 解决方法
查看>>
C++ Studio (二) ----- atoi()函数的实现 (自己编写功能)
查看>>