题目链接:http://www.lydsy.com/JudgeOnline/problem.php?id=2561
题目大意:给你一张图,让指定的边可能出现在最小生成树和最大生成树中,至少要删几条边
这个题很有意思,让指定的边不能出现在最小生成树中,那么边权比指定的边边权还小的边就不能使该边的两端点连通,两端点间的最小割就是使得该边可能加入最小生成树时删除的边的最少的边的数量;最大生成树同理
贴代码
#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <cstdlib>
#define INF 214748364
using namespace std;
struct Link
{
int s,t,c,next;
}l[1000005];
Link l2[1000005];
int g[20005];
int dist[20005];
bool b[20005];
queue<int> Q;
int n,m;
bool bfs(int x,int y)
{
Q.push(x);
dist[x] = 1;
while(!Q.empty())
{
int p = Q.front();
int w = g[p];
while(w)
{
if(l[w].c && !dist[l[w].t])
{
dist[l[w].t] = dist[p] + 1;
Q.push(l[w].t);
}
w = l[w].next;
}
Q.pop();
}
return dist[y];
}
int dfs(int x,int t,int flow)
{
if(x == t || flow == 0)
return flow;
int u = 0;
for(int i = g[x];i ; i = l[i].next)
{
if(l[i].c && dist[x] + 1 == dist[l[i].t])
{
int fl = dfs(l[i].t,t,min(l[i].c,flow - u));
u += fl;
l[i].c -= fl;
l[i^1].c += fl;
if(u == flow)
return flow;
}
}
if(u == 0)
dist[x] = INF;
return u;
}
int maxflow(int s,int t)
{
int flow = 0;
memset(dist,0,sizeof(dist));
while(bfs(s,t))
{
memset(b,0,sizeof(b));
int w = dfs(s,t,INF);
flow += w;
memset(b,0,sizeof(b));
memset(dist,0,sizeof(dist));
}
return flow;
}
void Add_Link(int s,int t,int c,int x)
{
l[x].s = s;
l[x].t = t;
l[x].c = c;
l[x].next = g[s];
g[s] = x;
l[x^1].s = t;
l[x^1].t = s;
l[x^1].c = c;
l[x^1].next = g[t];
g[t] = x^1;
return ;
}
int main()
{
int n,m,s,t,c,p = 0,ans = 0;;
scanf("%d%d",&n,&m);
for(int i = 1;i <= m;i ++)
scanf("%d%d%d",&l2[i].s,&l2[i].t,&l2[i].c);
scanf("%d%d%d",&s,&t,&c);
for(int i = 1;i <= m;i ++)
{
if(l2[i].c < c)
{
p += 2;
Add_Link(l2[i].s,l2[i].t,1,p);
}
}
ans += maxflow(s,t);
p = 0;
memset(l,0,sizeof(l));
memset(g,0,sizeof(g));
for(int i = 1;i <= m;i ++)
{
if(l2[i].c > c)
{
p += 2;
Add_Link(l2[i].s,l2[i].t,1,p);
}
}
ans += maxflow(s,t);
cout << ans;
return 0;
}