-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhdu_3251_Being a Hero_1.cpp
166 lines (164 loc) · 4.33 KB
/
hdu_3251_Being a Hero_1.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
#include <stdio.h>
#include <time.h>
#include <algorithm>
#include <map>
#include <string.h>
#include<queue>
#include<iostream>
#define N 60010
using namespace std;
const int maxn=1009;
const int inf=1<<30;
int n,m,f;
//**************************************************
//为dinic求最大流模版
struct edge
{
int v, next;
int val;
} net[ 200010 ];
int Qnum; //记录左侧点的数量
int level[maxn], Qu[maxn], out[maxn],next[maxn],ins[maxn],first[maxn];
class Dinic {
public:
int end;
int now;
int low,high;
Dinic() {
end = 0;
memset( next, -1, sizeof(next) );
}
inline void insert( int x, int y, int c) {
net[end].v = y, net[end].val = c,
net[end].next = next[x],
next[x] = end ++;
net[end].v = x, net[end].val = 0,
net[end].next = next[y],
next[y] = end ++;
}
bool BFS( int S, int E ) {
memset( level, -1, sizeof(level) );
low = 0, high = 1;
Qu[0] = S, level[S] = 0;
for( ; low < high; ) {
int x = Qu[low];
for( int i = next[x]; i != -1; i = net[i].next ) {
if( net[i].val == 0 ) continue;
int y = net[i].v;
if( level[y] == -1 ) {
level[y] = level[x] + 1;
Qu[ high ++] = y;
}
}
low ++;
}
return level[E] != -1;
}
int MaxFlow( int S, int E ){
int maxflow = 0;
for( ; BFS(S, E) ; ) {
memcpy( out, next, sizeof(out) );
now = -1;
for( ;; ) {
if( now < 0 ) {
int cur = out[S];
for(; cur != -1 ; cur = net[cur].next )
if( net[cur].val && out[net[cur].v] != -1 && level[net[cur].v] == 1 )
break;
if( cur >= 0 ) Qu[ ++now ] = cur, out[S] = net[cur].next;
else break;
}
int u = net[ Qu[now] ].v;
if( u == E ) {
int flow = inf;
int index = -1;
for( int i = 0; i <= now; i ++ ) {
if( flow > net[ Qu[i] ].val )
flow = net[ Qu[i] ].val, index = i;
}
maxflow += flow;
for( int i = 0; i <= now; i ++ )
net[Qu[i]].val -= flow, net[Qu[i]^1].val += flow;
for( int i = 0; i <= now; i ++ ) {
if( net[ Qu[i] ].val == 0 ) {
now = index - 1;
break;
}
}
}
else{
int cur = out[u];
for(; cur != -1; cur = net[cur].next )
if (net[cur].val && out[net[cur].v] != -1 && level[u] + 1 == level[net[cur].v])
break;
if( cur != -1 )
Qu[++ now] = cur, out[u] = net[cur].next;
else out[u] = -1, now --;
}
}
}
Qnum=high;//一定要记录此时在Qu数组里点的个数,方便后面枚举
return maxflow;
}
};
struct E
{
int v,next;
}edg[200011];
int ans;
void init(int k)
{
int u,v,w;
scanf("%d%d%d",&n,&m,&f);
memset(ins,0,sizeof(ins));
memset(first,-1,sizeof(first));
Dinic my;
int edg_num=1;
for(int i=1;i<=m;i++)
{
scanf("%d%d%d",&u,&v,&w);
edg[i].v=v;
edg[i].next=first[u];
first[u]=i;
my.insert(u,v,w);
}
int sum=0;
for(int i=0;i<f;i++)
{
scanf("%d%d",&u,&w);
my.insert(u,n+1,w);
sum+=w;
}
printf("Case %d: %d\n",k,sum-my.MaxFlow(1,n+1));
sum=0;
for(int i=0;i<Qnum;i++)
ins[Qu[i]]=1;//标记所有左侧的点
ans=0;
//dfs(1);
for(int i=0;i<Qnum;i++)
{
int x=Qu[i];
for(int j=first[x];j!=-1;j=edg[j].next)
{
if(!ins[edg[j].v])
out[ans++]=j;
}
}
printf("%d",ans);
for(int i=0;i<ans;i++)
{
printf(" %d",out[i]);
}
printf("\n");
}
int main()
{
int Case;
scanf("%d",&Case);
for(int i=1;i<=Case;i++)
{
init(i);
//solve();
}
return 0;
}