1. 程式人生 > >[Cqoi2014]危橋 (兩遍網路流)

[Cqoi2014]危橋 (兩遍網路流)

題目連結

  1 #include <bits/stdc++.h>
  2 using namespace std;
  3 typedef long long ll;
  4 inline int read()
  5 {
  6     int x=0,f=1;char ch=getchar();
  7     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
  8     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
9 return x*f; 10 } 11 12 /********************************************************************/ 13 14 #define inf 0xffffff 15 #define T 2001 16 const int maxn = 2e6+7; 17 const int Maxn = 2e3+5; 18 int a, b; 19 int ans1, ans2; 20 int head[Maxn], q[Maxn], dis[Maxn], from[Maxn];
21 bool vis[Maxn]; 22 23 struct node 24 { 25 int to, from, Next; 26 int v, c; 27 }e[maxn]; 28 int cnt = 1; 29 30 int gcd(int x, int y){ 31 if(y == 0) return x; 32 else return gcd(y, x%y); 33 } 34 35 void add_edge(int u, int v, int w, int c){ 36 e[++cnt].to = v; e[cnt].from
= u; e[cnt].Next = head[u]; head[u] = cnt; 37 e[cnt].v = w; e[cnt].c = c; 38 } 39 40 void insert(int u, int v, int w, int c){ 41 add_edge(u, v, w, c); 42 add_edge(v, u, 0, -c); 43 } 44 45 //是否滿足條件 46 bool check(int x, int y){ 47 if(x < y) swap(x, y); 48 int t = int(sqrt(x*x-y*y)); 49 return (gcd(y, t) == 1 && x*x-y*y == t*t); 50 } 51 52 bool spfa(){ 53 for(int i = 0;i <= T;i++){ 54 dis[i] = -inf; 55 } 56 int t = 0, w = 1; 57 dis[0] = 0; q[0] = 0; vis[0] = 1; 58 while(t != w){ 59 int now = q[t]; t++; 60 if(t == T) t = 0; 61 for(int i = head[now];i;i = e[i].Next){ 62 if(e[i].v && e[i].c+dis[now] > dis[e[i].to]){ 63 dis[e[i].to] = e[i].c + dis[now]; 64 from[e[i].to] = i; 65 if(!vis[e[i].to]){ 66 vis[e[i].to] = 1; 67 q[w++] = e[i].to; 68 if(w == T) w = 0; 69 } 70 } 71 } 72 vis[now] = 0; 73 } 74 if(dis[T] == -inf) return false; 75 return true; 76 } 77 78 void dfs(){ 79 int x = inf; 80 for(int i = from[T];i;i = from[e[i].from]){ 81 x = min(e[i].v, x); 82 } 83 for(int i = from[T];i;i = from[e[i].from]){ 84 ans2 += x*e[i].c; 85 e[i].v -= x; 86 e[i^1].v += x; 87 } 88 } 89 90 int main(){ 91 a = read(); b = read(); 92 for(int i = a;i <= b;i++){ 93 for(int j = a;j <= b;j++){ 94 if(check(i, j) && i != j){ 95 insert(i, j+1000, 1, i+j); 96 } 97 } 98 } 99 for(int i = a;i <= b;i++){ 100 insert(0, i, 1, 0); 101 insert(i+1000, T, 1, 0); 102 } 103 while(spfa()) dfs(); 104 for(int i = 2;i <= cnt;i += 2){ 105 if() 106 } 107 return 0; 108 }