Crazy Circuits HDU - 3157(有源汇有上下界最小流)

时间:2022-10-20 18:19:47

给出每条边的下界 求最小流

板题

提供两个板子代码 虽然这个题 第一个比较快

但在loj上https://loj.ac/problem/117

的板题  第一个1700+ms

第二个才600+ms  用输入挂后400+ms

#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff;
int n, m, s, t;
int head[maxn], cur[maxn], vis[maxn], nex[maxn << ], in[maxn];
int d[maxn];
int cnt;
struct node
{
int u, v;
int c;
}Node[maxn << ]; void add_(int u, int v, int c)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int c)
{
add_(u, v, c);
add_(v, u, );
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(!d[v] && Node[i].c > )
{
d[v] = d[u] + ;
Q.push(v);
if(v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] == d[u] + && Node[i].c > )
{
int V = dfs(v, min(cap, Node[i].c));
Node[i].c -= V;
Node[i ^ ].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic()
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof head);
ans += dfs(s, INF);
}
return ans;
} int tranf(string x)
{
if(x == "+") return ;
if(x == "-") return n + ;
int ans = , len = x.size();
for(int i = ; i < len; i++)
{
ans += (x[i] - '') * pow(, len - i - );
}
return ans + ;
} int main()
{
int w;
string u, v;
int u_, v_;
while(~scanf("%d%d", &n, &m))
{
if(n == && m == ) break;
mem(head, -);
cnt = ;
mem(in, );
int sum = ;
s = , t = n + ;
rap(i, , m)
{
// scanf(" %c %c %d", &u, &v, &w);
cin >> u >> v >> w;
u_ = tranf(u);
v_ = tranf(v);
add(u_, v_, INF - w);
in[v_] += w;
in[u_] -= w;
}
for(int i = ; i <= n + ; i++)
{
if(in[i] > )
{
sum += in[i];
add(s, i, in[i]);
}
else
add(i, t, -in[i]);
}
int ans = Dinic();
add(n + , , INF);
ans += Dinic();
if(sum == ans)
cout << Node[head[(n + )] ^ ].c << endl;
else
cout << "impossible" << endl; }
return ;
}
#include <iostream>
#include <cstdio>
#include <sstream>
#include <cstring>
#include <map>
#include <cctype>
#include <set>
#include <vector>
#include <stack>
#include <queue>
#include <algorithm>
#include <cmath>
#include <bitset>
#define rap(i, a, n) for(int i=a; i<=n; i++)
#define rep(i, a, n) for(int i=a; i<n; i++)
#define lap(i, a, n) for(int i=n; i>=a; i--)
#define lep(i, a, n) for(int i=n; i>a; i--)
#define rd(a) scanf("%d", &a)
#define rlld(a) scanf("%lld", &a)
#define rc(a) scanf("%c", &a)
#define rs(a) scanf("%s", a)
#define rb(a) scanf("%lf", &a)
#define rf(a) scanf("%f", &a)
#define pd(a) printf("%d\n", a)
#define plld(a) printf("%lld\n", a)
#define pc(a) printf("%c\n", a)
#define ps(a) printf("%s\n", a)
#define MOD 2018
#define LL long long
#define ULL unsigned long long
#define Pair pair<int, int>
#define mem(a, b) memset(a, b, sizeof(a))
#define _ ios_base::sync_with_stdio(0),cin.tie(0)
//freopen("1.txt", "r", stdin);
using namespace std;
const int maxn = 1e5 + , INF = 0x7fffffff;
int n, m, s, t;
int head[maxn], cur[maxn], vis[maxn], nex[maxn << ], in[maxn];
int d[maxn];
int cnt;
struct node
{
int u, v, bz;
int c;
}Node[maxn << ]; void add_(int u, int v, int c, int bz)
{
Node[cnt].u = u;
Node[cnt].v = v;
Node[cnt].c = c;
Node[cnt].bz = bz;
nex[cnt] = head[u];
head[u] = cnt++;
} void add(int u, int v, int c, int bz)
{
add_(u, v, c, bz);
add_(v, u, , bz);
} bool bfs()
{
queue<int> Q;
mem(d, );
Q.push(s);
d[s] = ;
while(!Q.empty())
{
int u = Q.front(); Q.pop();
for(int i = head[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(!d[v] && Node[i].c > )
{
d[v] = d[u] + ;
Q.push(v);
if(v == t) return ;
}
}
}
return d[t] != ;
} int dfs(int u, int cap)
{
int ret = ;
if(u == t || cap == )
return cap;
for(int &i = cur[u]; i != -; i = nex[i])
{
int v = Node[i].v;
if(d[v] == d[u] + && Node[i].c > )
{
int V = dfs(v, min(cap, Node[i].c));
Node[i].c -= V;
Node[i ^ ].c += V;
ret += V;
cap -= V;
if(cap == ) break;
}
}
if(cap > ) d[u] = -;
return ret;
} int Dinic()
{
int ans = ;
while(bfs())
{
memcpy(cur, head, sizeof head);
ans += dfs(s, INF);
}
return ans;
} int tranf(string x)
{
if(x == "+") return ;
if(x == "-") return n + ;
int ans = , len = x.size();
for(int i = ; i < len; i++)
{
ans += (x[i] - '') * pow(, len - i - );
}
return ans + ;
} int main()
{
int w;
string u, v;
int u_, v_;
while(~scanf("%d%d", &n, &m))
{
if(n == && m == ) break;
mem(head, -);
cnt = ;
mem(in, );
int sum = ;
s = , t = n + ;
rap(i, , m)
{
// scanf(" %c %c %d", &u, &v, &w);
cin >> u >> v >> w;
u_ = tranf(u);
v_ = tranf(v);
add(u_, v_, INF - w, );
in[v_] += w;
in[u_] -= w;
}
for(int i = ; i <= n + ; i++)
{
if(in[i] > )
{
sum += in[i];
add(s, i, in[i], );
}
else
add(i, t, -in[i], );
}
add(n + , , INF, );
if(sum != Dinic())
ps("impossible");
else
{
sum = Node[head[n + ] ^ ].c;
for(int i = ; i < cnt; i++)
{
if(!Node[i].bz) Node[i].v = ;
}
head[s] = head[t] = -;
s = n + ;
t = ;
cout << sum - Dinic() << endl;
} }
return ;
}