HDU 3251 Being a Hero(最小割+输出割边)

Problem Description
You are the hero who saved your country. As promised, the king will give you some cities of the country, and you can choose which ones to own!
But don't get too excited. The cities you take should NOT be reachable from the capital -- the king does not want to accidentally enter your area. In order to satisfy this condition, you have to destroy some roads. What's worse, you have to pay for that -- each road is associated with some positive cost. That is, your final income is the total value of the cities you take, minus the total cost of destroyed roads.
Note that each road is a unidirectional, i.e only one direction is available. Some cities are reserved for the king, so you cannot take any of them even if they're unreachable from the capital. The capital city is always the city number 1.

Input
The first line contains a single integer T (T <= 20), the number of test cases. Each case begins with three integers n, m, f (1 <= f < n <= 1000, 1 <= m < 100000), the number of cities, number of roads, and number of cities that you can take. Cities are numbered 1 to n. Each of the following m lines contains three integers u, v, w, denoting a road from city u to city v, with cost w. Each of the following f lines contains two integers u and w, denoting an available city u, with value w.

Output
For each test case, print the case number and the best final income in the first line. In the second line, print e, the number of roads you should destroy, followed by e integers, the IDs of the destroyed roads. Roads are numbered 1 to m in the same order they appear in the input. If there are more than one solution, any one will do.

Sample Input
2
4 4 2
1 2 2
1 3 3
3 2 4
2 4 1
2 3
4 4
4 4 2
1 2 2
1 3 3
3 2 1
2 4 1
2 3
4 4

Sample Output
Case 1: 3
1 4
Case 2: 4
2 1 3

题意

N个城市M条边F个城市,M行每行u,v,w代表破坏u到v需要花费w元,F行u,w代表1不能到城市u获得w元,问你最大利润然后输出破坏的道路编号

题解

建立源点S=1,汇点T=n+1,uv连边流量w,uT连边流量w,跑最小割,得到最小花费,用总钱-最小花费就是利润

输出路径考虑S和T集合,DFS(1)跑出来的是S集合的点,其余都是T集合的点,枚举m条边,如果u输出S集合,v属于T集合,就说明是割边

代码

 #include<bits/stdc++.h>
using namespace std; const int maxn=;
const int maxm=2e5+;//至少总M*2
const int INF=0x3f3f3f3f; int TO[maxm],CAP[maxm],NEXT[maxm],tote;
int FIR[maxn],gap[maxn],cur[maxn],d[maxn],q[],vis[maxn],u[maxm],v[maxm];
int n,m,S,T; void add(int u,int v,int cap)
{
//printf("i=%d %d %d %d\n",tote,u,v,cap);
TO[tote]=v;
CAP[tote]=cap;
NEXT[tote]=FIR[u];
FIR[u]=tote++; TO[tote]=u;
CAP[tote]=;
NEXT[tote]=FIR[v];
FIR[v]=tote++;
}
void bfs()
{
memset(gap,,sizeof gap);
memset(d,,sizeof d);
++gap[d[T]=];
for(int i=;i<=n;++i)cur[i]=FIR[i];
int head=,tail=;
q[]=T;
while(head<=tail)
{
int u=q[head++];
for(int v=FIR[u];v!=-;v=NEXT[v])
if(!d[TO[v]])
++gap[d[TO[v]]=d[u]+],q[++tail]=TO[v];
}
}
int dfs(int u,int fl)
{
if(u==T)return fl;
int flow=;
for(int &v=cur[u];v!=-;v=NEXT[v])
if(CAP[v]&&d[u]==d[TO[v]]+)
{
int Min=dfs(TO[v],min(fl,CAP[v]));
flow+=Min,fl-=Min,CAP[v]-=Min,CAP[v^]+=Min;
if(!fl)return flow;
}
if(!(--gap[d[u]]))d[S]=n+;
++gap[++d[u]],cur[u]=FIR[u];
return flow;
}
int ISAP()
{
bfs();
int ret=;
while(d[S]<=n)ret+=dfs(S,INF);
return ret;
}
void init()
{
tote=;
memset(FIR,-,sizeof FIR);
}
void dfs(int u)
{
for(int i=FIR[u];i!=-;i=NEXT[i])
{
int v=TO[i];
if(CAP[i]!=&&!vis[v])
{
vis[v]=true;
dfs(v);
}
}
}
int main()
{
int t,f,o=;
scanf("%d",&t);
while(t--)
{
init();
scanf("%d%d%d",&n,&m,&f);
for(int i=,w;i<m;i++)
{
scanf("%d%d%d",&u[i],&v[i],&w);
add(u[i],v[i],w);
}
S=,T=n+,n+=;
add(S,,INF);
int sum=;
for(int i=,uu,w;i<f;i++)
{
scanf("%d%d",&uu,&w);
add(uu,T,w);
sum+=w;
}
printf("Case %d: %d\n",o++,sum-ISAP()); memset(vis,,sizeof vis);
dfs();
vector<int>ans;
for(int i=;i<m;i++)
if(vis[u[i]]&&!vis[v[i]])
ans.push_back(i+);
printf("%d",(int)ans.size());
for(int i=;i<ans.size();i++)
printf(" %d",ans[i]);
printf("\n");
}
return ;
}
上一篇:Atitit.软件架构高扩展性and兼容性原理与概论实践attilax总结


下一篇:MSP430 使用时 RST引脚一定拉高