1 // another fine solution by misof
24 /////////////////// PRE-WRITTEN CODE FOLLOWS, LOOK DOWN FOR THE SOLUTION ////////////////////////////////
26 // pre-written code {{{
27 #define CLEAR(t) memset((t),0,sizeof(t))
28 #define FOR(i,a,b) for(int i=(int)(a);i<=(int)(b);++i)
29 typedef pair
<int,int> PII
;
30 #define REP(i,n) for(int i=0;i<(int)(n);++i)
31 #define SIZE(t) ((int)((t).size()))
34 /////////////////// CODE WRITTEN DURING THE COMPETITION FOLLOWS ////////////////////////////////
40 PII
MC_MAXFLOW(int N
, int source
, int sink
) {
43 int infinity
= 1; while (2*infinity
> infinity
) infinity
*= 2;
45 // speed optimization #1: adjacency graph
46 // speed optimization #2: cache the degrees
48 vector
<vector
<int> > G(N
);
49 for (int i
=0; i
<N
; i
++) for (int j
=0; j
<i
; j
++) if (CAP
[i
][j
]>0 || CAP
[j
][i
]>0) {
52 G
[i
].push_back(j
); G
[j
].push_back(i
);
55 vector
<int> potential(N
,0);
58 // use dijkstra to find an augmenting path
59 vector
<int> from(N
,-1);
60 vector
<int> dist(N
,infinity
);
62 priority_queue
< pair
<int,int>, vector
<pair
<int,int> >, greater
<pair
<int,int> > > Q
;
63 Q
.push(make_pair(0,source
));
64 from
[source
]=-2; dist
[source
] = 0;
67 int howFar
= Q
.top().first
;
68 int where
= Q
.top().second
;
71 if (dist
[where
] < howFar
) continue;
73 for (int i
=0; i
<deg
[where
]; i
++) {
74 int dest
= G
[where
][i
];
76 // now there are two possibilities: add flow in the right direction, or remove in the other one
77 if (flow
[dest
][where
] > 0)
78 if (dist
[dest
] > dist
[where
] + potential
[where
] - potential
[dest
] - COST
[dest
][where
]) {
79 dist
[dest
] = dist
[where
] + potential
[where
] - potential
[dest
] - COST
[dest
][where
];
81 Q
.push(make_pair(dist
[dest
],dest
));
84 if (flow
[where
][dest
] < CAP
[where
][dest
])
85 if (dist
[dest
] > dist
[where
] + potential
[where
] - potential
[dest
] + COST
[where
][dest
]) {
86 dist
[dest
] = dist
[where
] + potential
[where
] - potential
[dest
] + COST
[where
][dest
];
88 Q
.push(make_pair(dist
[dest
],dest
));
91 // no breaking here, we need the whole graph
95 // update vertex potentials
96 for (int i
=0; i
<N
; i
++) potential
[i
] += dist
[i
];
98 // if there is no path, we are done
99 if (from
[sink
] == -1) return make_pair(flowSize
,flowCost
);
101 // construct an augmenting path
102 int canPush
= infinity
;
106 int prev
=from
[where
];
108 if (flow
[where
][prev
])
109 canPush
= min( canPush
, flow
[where
][prev
] );
111 canPush
= min( canPush
, CAP
[prev
][where
] - flow
[prev
][where
] );
115 // update along the path
119 int prev
=from
[where
];
121 if (flow
[where
][prev
]) {
122 flow
[where
][prev
] -= canPush
;
123 flowCost
-= canPush
* COST
[where
][prev
];
125 flow
[prev
][where
] += canPush
;
126 flowCost
+= canPush
* COST
[prev
][where
];
132 return make_pair(-1,47);
139 for(int h
= 0; h
< casos
;h
++){
140 scanf("%i %i %i", &N
, &M
, &K
);
141 CLEAR(CAP
), CLEAR(COST
), CLEAR(flow
);
142 FOR(i
, 1, N
) CAP
[0][i
] = K
;
143 FOR(j
, N
+1, 2*N
) CAP
[j
][2*N
+1] = K
;
144 FOR(i
, 1, N
)FOR(j
, N
+1, 2*N
) CAP
[i
][j
] = 1;
147 // printf("%i %i %i\n", N, M, K);
148 FOR(i
, 1, N
)FOR(j
, N
+1, 2*N
) COST
[i
][j
] = 50000;
150 for(int i
=0;i
<M
;i
++){
152 scanf("%i %i %i", &f
, &t
, &d
);
156 pair
<int, int> p
= MC_MAXFLOW(2*N
+2, 0, 2*N
+1);
157 if( p
.first
!= K
*N
|| p
.second
>= 50000) printf("-1\n");
158 else printf("%i\n", p
.second
);
159 // printf(" %i %i\n", p.first, p.second);