標籤:
坑爹...要求最小值要轉成最長路來做....
小於關係要轉化一下 , A < B -> A <= B - 1
--------------------------------------------------------------------------
#include<cstdio>#include<cstring>#include<algorithm>#include<queue>#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 = 100009; struct edge {int to, w;edge*next;} E[maxn << 1], *pt = E, *head[maxn]; inline void add_edge(int u, int v, int w) {pt->to = v, pt->w = w;pt->next = head[u];head[u] = pt++;} bool inQ[maxn];int d[maxn], cnt[maxn], n;queue<int> Q; bool spfa() {rep(i, n) d[i] = 1, inQ[i] = true, Q.push(i), cnt[i] = 1;while(!Q.empty()) {int x = Q.front(); Q.pop();inQ[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(!inQ[e->to]) {if(++cnt[e->to] > n) return false;inQ[e->to] = true, Q.push(e->to);}}}return true;} int main() {freopen("test.in", "r", stdin);clr(head, 0);int k;cin >> n >> k;while(k--) {int x, a, b;scanf("%d%d%d", &x, &a, &b);a--, b--;switch(x) {case 1: add_edge(a, b, 0); add_edge(b, a, 0); break;case 2: add_edge(a, b, 1); break;case 3: add_edge(b, a, 0); break;case 4: add_edge(b, a, 1); break;case 5: add_edge(a, b, 0); break;default: break;}}if(!spfa()) puts("-1");else {long long ans = 0;rep(i, n) ans += d[i];printf("%lld\n", ans);}return 0;}
--------------------------------------------------------------------------
2330: [SCOI2011]糖果Time Limit: 10 Sec Memory Limit: 128 MB
Submit: 3064 Solved: 907
[Submit][Status][Discuss]Description
幼兒園裡有N個小朋友,lxhgww老師現在想要給這些小朋友們分配糖果,要求每個小朋友都要分到糖果。但是小朋友們也有嫉妒心,總是會提出一些要求,比如小明不希望小紅分到的糖果比他的多,於是在分配糖果的時候,lxhgww需要滿足小朋友們的K個要求。幼兒園的糖果總是有限的,lxhgww想知道他至少需要準備多少個糖果,才能使得每個小朋友都能夠分到糖果,並且滿足小朋友們所有的要求。
Input
輸入的第一行是兩個整數N,K。
接下來K行,表示這些點需要滿足的關係,每行3個數字,X,A,B。
如果X=1, 表示第A個小朋友分到的糖果必須和第B個小朋友分到的糖果一樣多;
如果X=2, 表示第A個小朋友分到的糖果必須少於第B個小朋友分到的糖果;
如果X=3, 表示第A個小朋友分到的糖果必須不少於第B個小朋友分到的糖果;
如果X=4, 表示第A個小朋友分到的糖果必須多於第B個小朋友分到的糖果;
如果X=5, 表示第A個小朋友分到的糖果必須不多於第B個小朋友分到的糖果;
Output
輸出一行,表示lxhgww老師至少需要準備的糖果數,如果不能滿足小朋友們的所有要求,就輸出-1。
Sample Input5 7
1 1 2
2 3 2
4 4 1
3 4 5
5 4 5
2 3 5
4 5 1
Sample Output
11
HINT
【資料範圍】
對於30%的資料,保證 N<=100
對於100%的資料,保證 N<=100000
對於所有的資料,保證 K<=100000,1<=X<=5,1<=A, B<=N
Source
Day1
BZOJ 2330: [SCOI2011]糖果( 差分約束 )