Submission #1293413
Source Code Expand
#include <cstdio>
#include <vector>
#include <queue>
#include <algorithm>
#include <utility>
template <class T>
using less_queue=std::priority_queue<T, std::deque<T>, std::greater<T>>;
struct SimpleEdge {
size_t src, dst;
SimpleEdge(size_t src, size_t dst): src(src), dst(dst) {}
};
template <class Weight>
struct WeightedEdge {
using weight_type=Weight;
size_t src, dst;
Weight cost;
WeightedEdge(size_t src, size_t dst, Weight cost):
src(src), dst(dst), cost(cost)
{}
};
template <class Weight>
struct FlowArc {
using weight_type=Weight;
size_t src, dst, rev;
Weight cost, capacity, flow;
FlowArc(size_t src, size_t dst, size_t rev, Weight cost, Weight capacity):
src(src), dst(dst), rev(rev), cost(cost), capacity(capacity), flow(0)
{}
Weight residue() const {
return capacity - flow;
}
};
template <class EdgeType>
struct Graph: public std::vector<std::vector<EdgeType>> {
using edge_type=EdgeType;
Graph(size_t v): std::vector<std::vector<EdgeType>>(v) {}
template <class... Attrs>
void connect_d(size_t src, size_t dst, Attrs ...attrs) {
(*this)[src].emplace_back(src, dst, attrs...);
}
template <class... Attrs>
void connect_u(size_t src, size_t dst, Attrs ...attrs) {
(*this)[src].emplace_back(src, dst, attrs...);
(*this)[dst].emplace_back(dst, src, attrs...);
}
template <class Weight=typename EdgeType::weight_type>
void connect_dr(size_t src, size_t dst, Weight cost, Weight capacity) {
size_t rev_s=(*this)[dst].size();
size_t rev_d=(*this)[src].size();
(*this)[src].emplace_back(src, dst, rev_s, cost, capacity);
(*this)[dst].emplace_back(dst, src, rev_d, 0, -capacity);
}
template <class... Attrs>
void connect_ur(size_t src, size_t dst, Attrs ...attrs) {
size_t rev_s=(*this)[dst].size();
size_t rev_d=(*this)[src].size();
(*this)[src].emplace_back(src, dst, rev_s, attrs...);
(*this)[dst].emplace_back(dst, src, rev_d, attrs...);
}
};
template <class Weight>
struct FlowNetwork: public Graph<FlowArc<Weight>> {
static const Weight INF=std::numeric_limits<Weight>::max()/4;
FlowArc<Weight> &reversed(FlowArc<Weight> &e) {
return (*this)[e.dst][e.rev];
}
FlowArc<Weight> &reversed(FlowArc<Weight> *e) {
return (*this)[e->dst][e->rev];
}
FlowNetwork(size_t v): Graph<FlowArc<Weight>>(v) {}
std::pair<Weight, Weight> mincflow(
size_t src, size_t snk, Weight fbound=INF
) {
size_t size=this->size();
std::vector<Weight> pot(size);
Weight maxflow=0, mincost=0;
while (fbound > maxflow) {
std::vector<Weight> d(size, INF);
d[src] = 0;
std::vector<FlowArc<Weight> *> prev(size, nullptr);
less_queue<std::pair<Weight, size_t>> q;
q.emplace(0, src);
while (!q.empty()) {
std::pair<Weight, size_t> p=q.top();
q.pop();
size_t v=p.second;
if (d[v] < p.first) continue;
for (FlowArc<Weight> &e: (*this)[v]) {
if (e.residue() <= 0) continue;
Weight cur=(d[e.src]+e.cost)+(pot[e.src]-pot[e.dst]);
if (d[e.dst] > cur) {
d[e.dst] = cur;
prev[e.dst] = &e;
q.emplace(d[e.dst], e.dst);
}
}
}
if (d[snk] == INF) break;
for (size_t v=0; v<size; ++v)
pot[v] += d[v];
Weight f=fbound-maxflow;
for (FlowArc<Weight> *e=prev[snk]; e!=nullptr; e=prev[e->src])
f = std::min(f, e->residue());
maxflow += f;
mincost += f * pot[snk];
for (FlowArc<Weight> *e=prev[snk]; e!=nullptr; e=prev[e->src]) {
e->flow += f;
reversed(e).flow -= f;
}
}
return {maxflow, mincost};
}
Weight maxflow(size_t src, size_t snk, Weight fbound=INF) {
size_t size=this->size();
std::vector<Weight> pot(size);
Weight maxflow=0;
while (fbound > maxflow) {
std::vector<Weight> d(size, INF);
d[src] = 0;
std::vector<FlowArc<Weight> *> prev(size, nullptr);
less_queue<std::pair<Weight, size_t>> q;
q.emplace(0, src);
while (!q.empty()) {
std::pair<Weight, size_t> p=q.top();
q.pop();
size_t v=p.second;
if (d[v] < p.first) continue;
for (FlowArc<Weight> &e: (*this)[v]) {
if (e.residue() <= 0) continue;
Weight cur=(d[e.src]+e.cost)+(pot[e.src]-pot[e.dst]);
if (d[e.dst] > cur) {
d[e.dst] = cur;
prev[e.dst] = &e;
q.emplace(d[e.dst], e.dst);
}
}
}
if (d[snk] == INF) break;
for (size_t v=0; v<size; ++v)
pot[v] += d[v];
Weight f=fbound-maxflow;
for (FlowArc<Weight> *e=prev[snk]; e!=nullptr; e=prev[e->src])
f = std::min(f, e->residue());
maxflow += f;
for (FlowArc<Weight> *e=prev[snk]; e!=nullptr; e=prev[e->src]) {
e->flow += f;
reversed(e).flow -= f;
}
}
return maxflow;
}
};
int main() {
size_t N;
int G, E;
scanf("%zu %d %d", &N, &G, &E);
FlowNetwork<int> g(N+1);
for (int i=0; i<G; ++i) {
size_t p;
scanf("%zu", &p);
g.connect_dr(p, N, 0, 1);
}
for (int i=0; i<E; ++i) {
size_t a, b;
scanf("%zu %zu", &a, &b);
g.connect_ur(a, b, 0, 1);
}
printf("%d\n", g.maxflow(0, N));
return 0;
}
Submission Info
Submission Time |
|
Task |
D - 浮気予防 |
User |
rsk0315 |
Language |
C++14 (GCC 5.4.1) |
Score |
100 |
Code Size |
6212 Byte |
Status |
AC |
Exec Time |
6 ms |
Memory |
768 KB |
Compile Error
./Main.cpp: In function ‘int main()’:
./Main.cpp:177:35: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%zu %d %d", &N, &G, &E);
^
./Main.cpp:182:25: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%zu", &p);
^
./Main.cpp:188:33: warning: ignoring return value of ‘int scanf(const char*, ...)’, declared with attribute warn_unused_result [-Wunused-result]
scanf("%zu %zu", &a, &b);
^
Judge Result
Set Name |
part |
All |
Score / Max Score |
99 / 99 |
1 / 1 |
Status |
|
|
Set Name |
Test Cases |
part |
test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_41_AB.txt, test_47_AB.txt |
All |
sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, sample_01.txt, sample_02.txt, sample_03.txt, sample_04.txt, sample_05.txt, test_01_AB.txt, test_02_AB.txt, test_03_AB.txt, test_04_AB.txt, test_05_AB.txt, test_06_AB.txt, test_07_AB.txt, test_08_AB.txt, test_09_AB.txt, test_10_AB.txt, test_11_AB.txt, test_12_AB.txt, test_13_AB.txt, test_14_AB.txt, test_15_AB.txt, test_16_AB.txt, test_17_AB.txt, test_18_AB.txt, test_19_AB.txt, test_20_AB.txt, test_21_AB.txt, test_22_AB.txt, test_23_AB.txt, test_24_AB.txt, test_25_AB.txt, test_26_A.txt, test_27_A.txt, test_28_A.txt, test_29_A.txt, test_30_A.txt, test_31_A.txt, test_32_A.txt, test_33_A.txt, test_34_A.txt, test_35_A.txt, test_36_A.txt, test_37_A.txt, test_38_A.txt, test_39_A.txt, test_40_A.txt, test_41_AB.txt, test_42_A.txt, test_43_A.txt, test_44_A.txt, test_45_A.txt, test_46_A.txt, test_47_AB.txt, test_48_A.txt, test_49_A.txt, test_50_A.txt, test_51_A.txt |
Case Name |
Status |
Exec Time |
Memory |
sample_01.txt |
AC |
1 ms |
256 KB |
sample_02.txt |
AC |
1 ms |
256 KB |
sample_03.txt |
AC |
1 ms |
256 KB |
sample_04.txt |
AC |
1 ms |
256 KB |
sample_05.txt |
AC |
1 ms |
256 KB |
test_01_AB.txt |
AC |
1 ms |
256 KB |
test_02_AB.txt |
AC |
1 ms |
256 KB |
test_03_AB.txt |
AC |
1 ms |
256 KB |
test_04_AB.txt |
AC |
1 ms |
256 KB |
test_05_AB.txt |
AC |
1 ms |
256 KB |
test_06_AB.txt |
AC |
1 ms |
256 KB |
test_07_AB.txt |
AC |
1 ms |
256 KB |
test_08_AB.txt |
AC |
1 ms |
256 KB |
test_09_AB.txt |
AC |
1 ms |
256 KB |
test_10_AB.txt |
AC |
1 ms |
256 KB |
test_11_AB.txt |
AC |
1 ms |
256 KB |
test_12_AB.txt |
AC |
1 ms |
256 KB |
test_13_AB.txt |
AC |
1 ms |
256 KB |
test_14_AB.txt |
AC |
1 ms |
256 KB |
test_15_AB.txt |
AC |
1 ms |
256 KB |
test_16_AB.txt |
AC |
1 ms |
256 KB |
test_17_AB.txt |
AC |
1 ms |
256 KB |
test_18_AB.txt |
AC |
1 ms |
256 KB |
test_19_AB.txt |
AC |
1 ms |
256 KB |
test_20_AB.txt |
AC |
1 ms |
256 KB |
test_21_AB.txt |
AC |
1 ms |
256 KB |
test_22_AB.txt |
AC |
1 ms |
256 KB |
test_23_AB.txt |
AC |
1 ms |
256 KB |
test_24_AB.txt |
AC |
1 ms |
256 KB |
test_25_AB.txt |
AC |
1 ms |
256 KB |
test_26_A.txt |
AC |
6 ms |
768 KB |
test_27_A.txt |
AC |
5 ms |
768 KB |
test_28_A.txt |
AC |
4 ms |
768 KB |
test_29_A.txt |
AC |
4 ms |
768 KB |
test_30_A.txt |
AC |
3 ms |
768 KB |
test_31_A.txt |
AC |
4 ms |
768 KB |
test_32_A.txt |
AC |
2 ms |
384 KB |
test_33_A.txt |
AC |
2 ms |
512 KB |
test_34_A.txt |
AC |
1 ms |
256 KB |
test_35_A.txt |
AC |
2 ms |
384 KB |
test_36_A.txt |
AC |
2 ms |
384 KB |
test_37_A.txt |
AC |
2 ms |
384 KB |
test_38_A.txt |
AC |
1 ms |
256 KB |
test_39_A.txt |
AC |
1 ms |
256 KB |
test_40_A.txt |
AC |
1 ms |
256 KB |
test_41_AB.txt |
AC |
1 ms |
256 KB |
test_42_A.txt |
AC |
2 ms |
384 KB |
test_43_A.txt |
AC |
1 ms |
256 KB |
test_44_A.txt |
AC |
2 ms |
384 KB |
test_45_A.txt |
AC |
1 ms |
256 KB |
test_46_A.txt |
AC |
1 ms |
256 KB |
test_47_AB.txt |
AC |
1 ms |
256 KB |
test_48_A.txt |
AC |
1 ms |
256 KB |
test_49_A.txt |
AC |
1 ms |
256 KB |
test_50_A.txt |
AC |
1 ms |
256 KB |
test_51_A.txt |
AC |
1 ms |
256 KB |