BZOJ 3436: 小K的农场( 差分约束 )
orz云神...
真的給跪了...BFS版spfa T 掉了...然后DFS版的就A了...我現在很迷茫....
這就是個普通的差分約束...?
------------------------------------------------------------------------------
#include<cstdio>#include<cstring>#include<algorithm>#include<stack>#include<iostream>#define rep(i, n) for(int i = 0; i < n; ++i)#define clr(x, c) memset(x, c, sizeof(x))using namespace std;const int maxn = 10010;struct edge {int to, w;edge*next;} E[maxn << 1], *pt = E, *head[maxn];inline void add_edge(int u, int v, int d) {pt->to = v, pt->w = d;pt->next = head[u];head[u] = pt++;}int d[maxn], inS[maxn], cnt[maxn], n;stack<int> S;bool spfa() {rep(i, n)S.push(i), inS[i] = true, cnt[i] = 1, d[i] = 0;while(!S.empty()) {int x = S.top(); S.pop();inS[x] = false;for(edge*e = head[x]; e; e = e->next) if(d[e->to] > d[x] + e->w) { ? ?d[e->to] = d[x] + e->w; ? ?if(!inS[e->to]) {if(++cnt[e->to] > n) return false; ? ? ? ?S.push(e->to), inS[e->to] = true; ? ?}}}return true;}inline int read() {char c = getchar();for(; !isdigit(c); c = getchar());int ans = 0;for(; isdigit(c); c = getchar()) ? ?ans = ans * 10 + c - '0';return ans;}void init() {clr(head, 0);n = read();int m = read();while(m--) {int t = read(), u = read() - 1, v = read() - 1;if(t == 3)? ? ?add_edge(u, v, 0), add_edge(v, u, 0);else {int w = read();t == 1 ? add_edge(u, v, -w) : add_edge(v, u, w);}}}int main(){freopen("test.in", "r", stdin);init();printf(spfa() ? "Yes\n" : "No\n");return 0;}???
------------------------------------------------------------------------------?
3436: 小K的農場
Time Limit:?10 Sec??Memory Limit:?128 MBSubmit:?351??Solved:?186
[Submit][Status][Discuss]
Description
?背景
??? 小K是個特么喜歡玩MC的孩紙。。。
?描述
??? 小K在MC里面建立很多很多的農場,總共n個,以至于他自己都忘記了每個農場中種植作物的具體數量了,他只記得一些含糊的信息(共m個),以下列三種形式描述:農場a比農場b至少多種植了c個單位的作物,農場a比農場b至多多種植了c個單位的作物,農場a與農場b種植的作物數一樣多。但是,由于小K的記憶有些偏差,所以他想要知道存不存在一種情況,使得農場的種植作物數量與他記憶中的所有信息吻合。輸入格式
?
Input
?? 第一行包括兩個整數n和m,分別表示農場數目和小K記憶中的信息的數目
??? 接下來m行:
??? 如果每行的第一個數是1,接下來有三個整數a,b,c,表示農場a比農場b至少多種植了c個單位的作物??? 如果每行第一個數是2,接下來有三個整數a,b,c,表示農場a比農場b至多多種植了c個單位的作物
??? 如果每行第一個數是3,接下來有兩個整數a,b,表示農場a種植的數量與b一樣多輸出格式
Output
??? 如果存在某種情況與小K的記憶吻合,輸出”Yes”,否則輸出”No”
Sample Input
33312
1131
2232
Sample Output
Yes
樣例解釋
三個農場種植的數量可以為(2,2,1)。
對于100%的數據,1<=n,m,a,b,c<=10000
HINT
Source
Kpmcup#0 By Greens
?
轉載于:https://www.cnblogs.com/JSZX11556/p/4657366.html
總結
以上是生活随笔為你收集整理的BZOJ 3436: 小K的农场( 差分约束 )的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: DIV+CSS 入门
- 下一篇: [转]hadoop新手错误解决方法