Search is not available for this dataset
name
stringlengths
2
88
description
stringlengths
31
8.62k
public_tests
dict
private_tests
dict
solution_type
stringclasses
2 values
programming_language
stringclasses
5 values
solution
stringlengths
1
983k
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct FordFulkerson { struct Edge { int to, cap, rev; Edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {} }; int V; vector<vector<Edge> > G; vector<bool> used; FordFulkerson(int V) : V(V) { G.resize(V); used.assign(V, false); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (1) { used.assign(V, false); int f = dfs(s, t, INF); if (f == 0) break; flow += f; } return flow; } }; int n, m; void solve() { int sum = 0; vector<pair<pair<int, int>, int> > vec; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) vec.push_back(pair<pair<int, int>, int>(make_pair(x, y), c)); else sum += c; } if (vec.size() == 1) { cout << sum + vec[0].second << endl; return; } else if (vec.size() == 0) { cout << sum << endl; return; } int ans = INF; for (int i = 0; i < (n); i++) { if (!i) continue; FordFulkerson flow(n); for (int j = 0; j < (vec.size()); j++) { flow.add_edge(vec[j].first.first, vec[j].first.second, vec[j].second); flow.add_edge(vec[j].first.second, vec[j].first.first, vec[j].second); } ans = min(ans, sum + flow.max_flow(0, i)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; long long flow; Node(int to, int idx, long long flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; long long dfs(int src, long long flow, int sink) { if (src == sink) return flow; long long res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; long long d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, long long flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } long long compute_maxflow(int src, int sink) { long long res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); long long tmp = dfs(src, numeric_limits<long long>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; class UnionFindTree { private: int* par; int* rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); long long bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; long long cost; scanf("%d %d %lld", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0LL, cost)); uft.unite(from, to); } long long res = numeric_limits<long long>::max(); for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } printf("%lld\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct node { int u, v, c; int next; } edge[150 * 150]; int mat[150][150], vis[150][150]; int e, head[150], ans; int n, m, inde, dfn[150], low[150]; int fa[150]; void unit(int f, int t, int c) { edge[e].u = f; edge[e].v = t; edge[e].c = c; edge[e].next = head[f]; head[f] = e++; } void tarjan(int u, int father) { fa[u] = father; int i; low[u] = dfn[u] = ++inde; for (i = head[u]; i != -1; i = edge[i].next) { int v = edge[i].v; if (dfn[v] == -1) { tarjan(v, u); low[u] = low[u] < low[v] ? low[u] : low[v]; } else if (father != v) { low[u] = low[u] < dfn[v] ? low[u] : dfn[v]; } } } void solve() { inde = 0; int i; for (i = 0; i <= n; i++) { dfn[i] = -1; fa[i] = -1; } tarjan(0, -1); for (i = 0; i < n; i++) { int v = fa[i]; if (v >= 0 && dfn[v] < low[i]) { ans = ans < mat[v][i] ? ans : mat[v][i]; } } } int main() { int i, j; while (scanf("%d%d", &n, &m), n + m) { e = 0; memset(head, -1, sizeof(head)); for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) mat[i][j] = vis[i][j] = 0; for (i = 0; i < m; i++) { int f, t, c; scanf("%d%d%d", &f, &t, &c); mat[f][t] += c; mat[t][f] += c; vis[f][t] = 1; vis[t][f] = 1; } for (i = 0; i <= n; i++) for (j = 0; j <= n; j++) if (vis[i][j] != 0) { unit(i, j, mat[i][j]); } ans = 0xfffffff; solve(); printf("%d\n", ans); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; void add_edge(vector<vector<Edge> >& G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, 0, G[a].size() - 1)); } int dfs(vector<vector<Edge> >& G, int u, int t, int f, vector<bool>& used) { if (u == t) return f; used[u] = true; for (__typeof((G[u]).begin()) e = (G[u]).begin(); e != (G[u]).end(); ++e) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(vector<vector<Edge> > G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { vector<vector<Edge> > G(N); int minus = 0; for (int i = (0); i < (int)(M); ++i) { int a, b, c; cin >> a >> b >> c; if (c <= 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; for (int s = (0); s < (int)(N); ++s) for (int t = (s + 1); t < (int)(N); ++t) { ans = min(ans, max_flow(G, s, t)); } cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); int lhs = tmp.compute_maxflow(0, house_i); FordFulkerson tmp2(ff); int rhs = tmp2.compute_maxflow(house_i, 0); res = min(res, max(lhs, rhs)); } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct UnionFind { int size_; std::vector<int> par; std::vector<int> rank; UnionFind(int size_) : size_(size_) { par.resize(size_); rank.resize(size_); for (int i = 0; i < size_; i++) { par[i] = i; rank[i] = 0; } } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int n, m; int kruskal(int v, const vector<pair<int, pair<int, int> > > &vec) { UnionFind uf(n); int ret = 0; for (int i = 0; i < (vec.size()); i++) { pair<int, pair<int, int> > pi = vec[i]; if (pi.second.first == v || pi.second.second == v) continue; if (!uf.same(pi.second.first, pi.second.second)) { uf.unite(pi.second.first, pi.second.second); ret += pi.first; } } return ret; } void solve() { vector<pair<int, pair<int, int> > > vec; int s = 0; int ms = 0; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) { s += c; vec.push_back(pair<int, pair<int, int> >(c, make_pair(x, y))); } else ms += c; } int ans = INF; sort((vec).begin(), (vec).end(), [](pair<int, pair<int, int> > p1, pair<int, pair<int, int> > p2) { return p1.first > p2.first; }); for (int i = 0; i < (n); i++) { ans = min(ans, ms + s - kruskal(i, vec)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } int bsr(int x) { return 31 - __builtin_clz(x); } template <class E> using Graph = vector<vector<E>>; template <class D, D INF> struct BidirectedCut { D sum = INF; template <class E> BidirectedCut(Graph<E> g) { int n = (int)g.size(); int m_a = -1, m_b = -1; vector<D> dist_base(n, 0); for (int m = n; m > 1; m--) { int a, b; auto dist = dist_base; for (int i = 0; i < m; i++) { a = b; b = max_element(begin(dist), end(dist)) - begin(dist); if (i == m - 1) sum = min(sum, dist[b]); dist[b] = -INF; for (E &e : g[b]) { if (e.to == m_b) e.to = m_a; if (dist[e.to] == -INF) continue; dist[e.to] += e.dist; } } m_a = a; m_b = b; g[a].insert(end(g[a]), begin(g[b]), end(g[b])); g[b].clear(); dist_base[b] = -INF; } } }; template <class C, C INF> struct MaxFlow { int V; vector<int> level, iter; template <class E> C exec(Graph<E> &g, int s, int t) { V = (int)g.size(); level.resize(V); iter.resize(V); C flow = 0; while (true) { bfs(g, s); if (level[t] < 0) return flow; fill_n(iter.begin(), V, 0); while (true) { C f = dfs(g, s, t, INF); if (!f) break; flow += f; } } } template <class E> void bfs(const Graph<E> &g, int s) { fill_n(level.begin(), V, -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (E e : g[v]) { if (e.cap <= 0) continue; if (level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } template <class E> C dfs(Graph<E> &g, int v, int t, C f) { if (v == t) return f; C res = 0; for (int &i = iter[v]; i < (int)g[v].size(); i++) { E &e = g[v][i]; if (e.cap <= 0) continue; if (level[v] < level[e.to]) { C d = dfs(g, e.to, t, min(f, e.cap)); if (d <= 0) continue; e.cap -= d; g[e.to][e.rev].cap += d; return d; } } return res; } }; int rand_int(int l, int r) { using D = uniform_int_distribution<int>; static random_device rd; static mt19937 gen(rd()); return D(l, r - 1)(gen); } bool solve() { int n, m; cin >> n >> m; if (!n) return false; struct Edge { int to; ll dist; }; Graph<Edge> g(n); auto addEdge = [&](int a, int b, ll c) { g[a].push_back(Edge{b, c}); g[b].push_back(Edge{a, c}); }; for (int i = 0; i < m; i++) { int a, b; ll c; cin >> a >> b >> c; addEdge(a, b, c); } cout << BidirectedCut<ll, TEN(15)>(g).sum << endl; return true; } int main() { while (solve()) { } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long MAX = LONG_LONG_MAX; long long V, E; bool used[101]; long long cnt; struct Edge { long long to, cost; Edge() {} Edge(long long _t, long long _c) : to(_t), cost(_c) {} }; vector<Edge> G[101]; int f, t; void check(long long x) { if (used[x]) return; used[x] = true; cnt++; for (long long i = 0; i < G[x].size(); i++) { long long to = G[x][i].to; if (f == i && t == to) continue; if (used[to]) continue; check(to); } } int main() { while (cin >> V >> E, V | E) { long long ans = 0; fill(used, used + 101, false); for (long long i = 0; i < 101; i++) G[i].clear(); for (long long i = 0; i < E; i++) { long long from, to, cost; cin >> from >> to >> cost; if (cost <= 0) ans += cost; else { G[from].push_back(Edge(to, cost)); G[to].push_back(Edge(from, cost)); } } f = t = -1; cnt = 0; check(0); long long tmp = ans; if (cnt == V) { long long value = 1 << 29; for (long long i = 0; i < V; i++) { long long tmp = 0; for (long long j = 0; j < G[i].size(); j++) { tmp += G[i][j].cost; } value = min(value, tmp); } tmp += value; } for (long long i = 0; i < V; i++) { long long value = 1 << 29; for (long long j = 0; j < G[i].size(); j++) { cnt = 0; fill(used, used + 101, false); f = i, t = G[i][j].to; check(0); if (cnt != V) value = min(value, G[i][j].to); } ans += value; } cout << min(ans, tmp) << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxN = 110; const int oo = 2147483647; int n, m; struct EE { int v, ne, c; } e[100010]; struct XX { int a, b, c; } x[10010]; int e_si, st, ed, N, head[maxN], cur[maxN], low[maxN], pre[maxN], pree[maxN], d[maxN], gap[maxN]; void adde(int a, int b, int c1, int c2) { e_si++; e[e_si].v = b; e[e_si].c = c1; e[e_si].ne = head[a]; head[a] = e_si; e_si++; e[e_si].v = a; e[e_si].c = c2; e[e_si].ne = head[b]; head[b] = e_si; } int sap() { int ret = 0; for (int i = 0; i <= N; i++) { d[i] = 0; gap[i] = 0; cur[i] = head[i]; } low[st] = oo; gap[0] = N; int u = st; while (d[st] < N) { bool fail = true; for (int i = cur[u]; i != -1; i = e[i].ne) { int v = e[i].v; cur[u] = i; if (e[i].c && d[u] == d[v] + 1) { pre[v] = u; pree[v] = i; low[v] = min(low[u], e[i].c); u = v; if (u == ed) { do { e[pree[u]].c -= low[ed]; e[pree[u] ^ 1].c += low[ed]; u = pre[u]; } while (u != st); ret += low[ed]; } fail = false; break; } } if (fail) { gap[d[u]]--; if (!gap[d[u]]) return ret; d[u] = N; for (int i = head[u]; i != -1; i = e[i].ne) if (e[i].c) d[u] = min(d[u], d[e[i].v] + 1); gap[d[u]]++; cur[u] = head[u]; if (u != st) u = pre[u]; } } return ret; } int main() { scanf("%d%d", &n, &m); while (n != 0 || m != 0) { st = n; ed = n + 1; N = ed + 1; int ans = -1; for (int i = 0; i < m; i++) { scanf("%d%d%d", &x[i].a, &x[i].b, &x[i].c); } for (int i = 1; i < n; i++) { e_si = -1; for (int j = 0; j <= N; j++) head[j] = -1; adde(st, 0, oo, 0); adde(i, ed, oo, 0); for (int j = 0; j < m; j++) adde(x[j].a, x[j].b, x[j].c, x[j].c); int tmp = sap(); if (ans == -1 || tmp < ans) ans = tmp; } printf("%d\n", ans); scanf("%d%d", &n, &m); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; int N, M; vector<Edge> G[110], cycle; int color[110]; void dfs(int v, vector<Edge> u) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { Edge &e = G[v][i]; int to = e.to; if (color[to] == -1) { u.push_back(G[v][i]); dfs(to, u); u.pop_back(); } else if (color[to] == 0) { color[to] = 1; u.push_back(G[v][i]); cycle = u; u.pop_back(); throw 0; } } color[v] = 1; } vector<vector<Edge>> cycles; bool solve() { vector<Edge> vec; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { try { if (color[i] == -1) { dfs(i, vec); } } catch (...) { cycles.push_back(cycle); cycle.clear(); } vec.clear(); } return (cycles.size() > 0); } void init() { cycle.clear(); cycles.clear(); for (int i = 0; i < 110; i++) { G[i].clear(); } } class Union_Find { public: int par[110], rnk[110], size[110], gnum; Union_Find(int N) { gnum = N; for (int i = 0; i < N; i++) { par[i] = i; rnk[i] = 0; size[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; size[y] += size[x]; } else { par[y] = x; size[x] += size[y]; if (rnk[x] == rnk[y]) { rnk[x]++; } } gnum--; } bool same(int x, int y) { return (find(x) == find(y)); } int getSize(int x) { return size[find(x)]; } int groups() { return gnum; } }; int main() { int s, t, cost; while (cin >> N >> M, N) { int min_cost = INT_MAX; Union_Find uf(N); init(); for (int i = 0; i < M; i++) { cin >> s >> t >> cost; G[s].push_back(Edge(s, t, cost)); min_cost = min(min_cost, cost); uf.unite(s, t); } if (solve()) { set<pair<int, int>> st; vector<int> v; if (cycles.size() == 1) { for (int i = 0; i < (int)cycles[0].size(); i++) { v.push_back(cycles[0][i].cost); st.insert(pair<int, int>(cycles[0][i].from, cycles[0][i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); i++) { Edge &e = G[i][j]; if (st.count(pair<int, int>(e.from, e.to)) == 0) { min_cost = min(min_cost, e.cost + min(0, v[0])); } } } } else { min_cost = 0; for (int i = 0; i < (int)cycles.size(); i++) { vector<int> v; for (int j = 0; j < (int)cycles[i].size(); j++) { v.push_back(cycles[i][j].cost); } sort(v.begin(), v.end()); if (v[0] < 0) { min_cost += v[0]; } } } } else { if (uf.gnum != 1) { min_cost = 0; } } cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; int next_flow = min(flow, e.flow); if (next_flow <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= next_flow; rev_e.flow += next_flow; res = max(res, dfs(e.to, next_flow, sink)); } return res; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(0, house_i)); } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct FordFulkerson { struct Edge { int to, cap, rev; Edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {} }; int V; vector<vector<Edge> > G; vector<bool> used; FordFulkerson(int V) : V(V) { G.resize(V); used.assign(V, false); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (1) { used.assign(V, false); int f = dfs(s, t, INF); if (f == 0) break; flow += f; } return flow; } }; int n, m; void solve() { int sum = 0; vector<pair<pair<int, int>, int> > vec; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) vec.push_back(pair<pair<int, int>, int>(make_pair(x, y), c)); else sum += c; } if (vec.size() == 1) { cout << sum + vec[0].second << endl; return; } else if (vec.size() == 0) { cout << sum << endl; return; } int ans = INF; for (int i = 0; i < (vec.size()); i++) { FordFulkerson flow(n); flow.add_edge(vec[i].first.first, vec[i].first.second, vec[i].second); flow.add_edge(vec[i].first.second, vec[i].first.first, vec[i].second); ans = min(ans, sum + flow.max_flow(0, vec[i].first.second)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; int N, M; bool found; vector<Edge> G[110], cycle; int color[110]; void dfs(int v, vector<Edge> u) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { Edge &e = G[v][i]; int to = e.to; if (color[to] == -1) { u.push_back(G[v][i]); dfs(to, u); u.pop_back(); } else if (color[to] == 0) { u.push_back(G[v][i]); cycle = u; u.pop_back(); throw 0; } } color[v] = 1; } bool solve() { vector<Edge> vec; found = false; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { try { if (color[i] == -1) { dfs(i, vec); } } catch (...) { break; } } return (cycle.size() > 0); } void init() { for (int i = 0; i < 110; i++) { G[i].clear(); } } int main() { int s, t, cost; while (cin >> N >> M, N) { int min_cost = INT_MAX; for (int i = 0; i < M; i++) { cin >> s >> t >> cost; G[s].push_back(Edge(s, t, cost)); min_cost = min(min_cost, cost); } if (solve()) { set<pair<int, int> > st; vector<int> v; for (int i = 0; i < (int)cycle.size(); i++) { v.push_back(cycle[i].cost); st.insert(pair<int, int>(cycle[i].from, cycle[i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); i++) { Edge &e = G[i][j]; if (st.count(pair<int, int>(e.from, e.to)) == 0) { min_cost = min(min_cost, e.cost + min(0, v[0])); } } } } cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; class UnionFindTree { private: int* par; int* rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } int res = numeric_limits<int>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int maxint = -1u >> 1; const int maxn = 100 + 10; int g[maxn][maxn], n, m; int a[maxn], v[maxn], w[maxn]; int minCut() { int ans = (-1u) >> 1; for (int i = 0; i < n; ++i) { v[i] = i; } while (n > 1) { int maxi = 1; a[v[0]] = 1; for (int i = 1; i < n; ++i) { a[v[i]] = 0; w[i] = g[v[0]][v[i]]; if (w[i] > w[maxi]) { maxi = i; } } int prev = 0, buf = n; while (--buf) { a[v[maxi]] = 1; if (buf == 1) { if (ans > w[maxi]) { ans = w[maxi]; } for (int k = 0; k < n; ++k) { g[v[k]][v[prev]] = g[v[prev]][v[k]] = g[v[prev]][v[k]] + g[v[maxi]][v[k]]; } --n; v[maxi] = v[n]; } prev = maxi; maxi = -1; for (int j = 1; j < n; ++j) { if (!a[v[j]]) { w[j] += g[v[prev]][v[j]]; if (maxi < 0 || w[j] > v[maxi]) { maxi = j; } } } } } return ans; } void gao() { int ans = 0; memset(g, 0, sizeof(g)); for (int i = 0, a, b, c; i < m; ++i) { scanf("%d%d%d", &a, &b, &c); if (c < 0) { ans += c; } else { g[a][b] = g[b][a] = g[a][b] + c; } } printf("%d\n", ans + minCut()); } int main() { while (scanf("%d%d", &n, &m), n || m) { gao(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int next_flow = min(flow, e.flow); Node& rev_e = _graph[e.to][e.idx]; e.flow -= next_flow; rev_e.flow += next_flow; res = max(res, dfs(e.to, next_flow, sink)); } return res; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(0, house_i)); } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; template <class T> struct AdjMatrix : public vector<vector<T> > { AdjMatrix(int n, const vector<T> &v = vector<T>()) : vector<vector<T> >(n, v) {} }; template <class T> T augment(const AdjMatrix<T> &capa, int src, int snk, AdjMatrix<T> &flow) { int n = capa.size(); vector<int> parent(n, -1); parent[src] = -2; T water = 0; queue<pair<T, int> > qu; qu.push(make_pair(INF, src)); while (!qu.empty()) { pair<T, int> a = qu.front(); qu.pop(); int u = a.second; T w = a.first; if (u == snk) { water = w; break; } for (int v = 0; v < (n); v++) if (parent[v] == -1 && capa[u][v] - flow[u][v] > 0) { qu.push(make_pair(min(w, capa[u][v] - flow[u][v]), v)); parent[v] = u; } } if (water == 0) return 0; for (int v = snk, u = parent[snk]; v != src; v = u, u = parent[u]) { flow[u][v] += water; flow[v][u] -= water; } return water; } template <class T> T FordFulkerson(const AdjMatrix<T> &capa, int src, int snk) { int n = capa.size(); AdjMatrix<T> flow(n, vector<T>(n)); T ans = 0; while (1) { T water = augment(capa, src, snk, flow); if (water == 0) break; ans += water; } return ans; } int main() { for (int n, m; scanf("%d%d", &n, &m), n;) { int ans = 0; AdjMatrix<int> adj(n); for (int i = 0; i < (m); i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); if (w < 0) ans += w; adj[u][v] = adj[v][u] = max(w, 0); } int ans2 = INF; for (int v = 0; v < (n); v++) for (int u = 0; u < (v); u++) ans2 = min(ans2, FordFulkerson(adj, u, v)); printf("%d\n", ans + ans2); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct FordFulkerson { struct Edge { int to, cap, rev; Edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {} }; int V; vector<vector<Edge> > G; vector<bool> used; FordFulkerson(int V) : V(V) { G.resize(V); used.assign(V, false); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (1) { used.assign(V, false); int f = dfs(s, t, INF); if (f == 0) break; flow += f; } return flow; } }; int n, m; void solve() { int s = n; int t = n + 1; int sum = 0; vector<pair<pair<int, int>, int> > vec; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) vec.push_back(pair<pair<int, int>, int>(make_pair(x, y), c)); else sum += c; } if (vec.size() == 1) { cout << vec[0].second << endl; return; } else if (vec.size() == 0) { cout << sum << endl; return; } int ans = INF; for (int i = 0; i < (vec.size()); i++) { FordFulkerson flow(t + 1); flow.add_edge(s, vec[i].first.first, INF); for (int j = 0; j < (m); j++) { flow.add_edge(vec[j].first.first, vec[j].first.second, vec[j].second); flow.add_edge(vec[j].first.second, vec[j].first.first, vec[j].second); if (i != j) flow.add_edge(vec[j].first.second, t, INF); } ans = min(ans, sum + flow.max_flow(s, t)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; void add_edge(vector<vector<Edge> >& G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, 0, G[a].size() - 1)); } int dfs(vector<vector<Edge> >& G, int u, int t, int f, vector<bool>& used) { if (u == t) return f; used[u] = true; for (__typeof((G[u]).begin()) e = (G[u]).begin(); e != (G[u]).end(); ++e) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(vector<vector<Edge> > G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { vector<vector<Edge> > G(N); int minus = 0; for (int i = (0); i < (int)(M); ++i) { int a, b, c; cin >> a >> b >> c; if (c < 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; for (int t = (1); t < (int)(N); ++t) { ans = min(ans, max_flow(G, 0, t)); } cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int s[1005]; int main(int argc, char** argv) { int n, m, i; while (scanf("%d %d", &n, &m), n + m) { int a, b; for (i = 0; i < m; i++) scanf("%d %d %d", a, b, &s[i]); sort(s, s + m); printf("%d\n", s[0]); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int i; int n, m; while (cin >> n >> m && n + m) { int b[100]; for (i = 0; i < n; i++) b[i] = i; priority_queue<pair<int, pair<int, int> > > c; for (i = 0; i < m; i++) { int p, q, r; cin >> p >> q >> r; c.push(make_pair(r, make_pair(p, q))); } int o = n; int sm = 0; while (c.empty() == 0) { int x, y, z; z = c.top().first; x = c.top().second.first; y = c.top().second.second; c.pop(); if (b[x] != b[y]) { if (o > 2) { for (i = 0; i < n; i++) { if (i != y && b[i] == b[y]) b[i] = b[x]; } b[y] = b[x]; o--; } else sm += z; } } cout << sm << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0, mn = 1234567890123456789LL, mn1 = 1234567890123456789; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c < 0) sum += c; if (mn >= c) { mn1 = mn; mn = c; } } if (mn < 0) { cout << sum << endl; continue; } if (n - 1 > m) { cout << 0 << endl; continue; } if (n - 1 == m) { cout << mn << endl; continue; } cout << mn + mn1 << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long par[100], rnk[100]; void init(long long N) { for (long long i = 0; i < N; i++) { par[i] = i; rnk[i] = 0; } } long long find(long long x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(long long x, long long y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; } else { par[y] = x; if (rnk[y] == rnk[x]) { rnk[x]++; } } } bool same(long long x, long long y) { return (find(x) == find(y)); } struct Edge { long long u, v, cost; }; bool comp(const Edge &e1, const Edge &e2) { return e1.cost > e2.cost; } Edge es[10000]; long long V, E, min_cost; long long kruskal() { sort(es, es + E, comp); init(V); long long res = 0; for (long long i = 0; i < E; i++) { Edge &e = es[i]; if (!same(e.u, e.v)) { unite(e.u, e.v); min_cost = min(min_cost, e.cost); res += e.cost; } } return res; } int main() { while (cin >> V >> E, V) { long long sum = 0; for (long long i = 0; i < E; i++) { cin >> es[i].u >> es[i].v >> es[i].cost; sum += es[i].cost; } min_cost = LLONG_MAX; cout << sum - kruskal() + min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int next_flow = min(flow, e.flow); int d = dfs(e.to, next_flow, sink); if (d <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= next_flow; rev_e.flow += next_flow; res = max(res, d); } return res; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, flow)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; class UnionFindTree { private: int* par; int* rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } int res = numeric_limits<int>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct { int to, next; } edge[100005]; int head[10010], stack[10010], DFN[10010], Low[10010], Belong[100010]; int instack[10010], cnt, scnt, top, n, m, tot; void Add(int x, int y) { edge[tot].to = y; edge[tot].next = head[x]; head[x] = tot++; } void Tarjan(int v) { int min, i, t, j; DFN[v] = Low[v] = ++cnt; instack[v] = 1; stack[top++] = v; for (i = head[v]; i != -1; i = edge[i].next) { j = edge[i].to; if (!DFN[j]) { Tarjan(j); if (Low[v] > Low[j]) Low[v] = Low[j]; } else if (instack[j] && DFN[j] < Low[v]) { Low[v] = DFN[j]; } } if (DFN[v] == Low[v]) { scnt++; do { t = stack[--top]; instack[t] = 0; Belong[t] = scnt; } while (t != v); } } void Slove() { int i; scnt = cnt = top = 0; memset(DFN, 0, sizeof(DFN)); for (i = 1; i <= n; i++) if (!DFN[i]) Tarjan(i); } long long ans = 0; int sum; long long a1, a2; void get_data() { ans = 0; a1 = (long long)1 << 40, a2 = (long long)1 << 40; sum = 0; int u, v; long long w; while (m--) { scanf("%d%d%lld", &u, &v, &w); if (w <= 0) { sum++; ans += w; } if (w < a2) { a2 = w; if (a2 < a1) swap(a1, a2); } Add(u + 1, v + 1); } } long long mi(long long a, long long b) { return a < b ? a : b; } int main() { int nn; while (cin >> n >> m && (n || m)) { memset(head, -1, sizeof(head)); tot = 0; get_data(); Slove(); if (scnt == 1) { if (sum >= 2) printf("%lld\n", mi(a1 + a2, ans)); else { printf("%lld\n", a1 + a2); } } else { printf("%lld\n", ans); } } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T> void debug(T begin, T end) { for (T i = begin; i != end; ++i) cerr << *i << " "; cerr << endl; } inline bool valid(int x, int y, int W, int H) { return (x >= 0 && y >= 0 && x < W && y < H); } const int INF = 100000000; const double EPS = 1e-8; const int MOD = 1000000007; int dx[8] = {1, 0, -1, 0, 1, -1, -1, 1}; int dy[8] = {0, 1, 0, -1, 1, 1, -1, -1}; struct Edge { int src, dst, cap, rev; Edge(int u, int v, int c, int r) : src(u), dst(v), cap(c), rev(r) {} }; void add_edge(vector<vector<Edge> >& G, int a, int b, int c) { G[a].push_back(Edge(a, b, c, G[b].size())); G[b].push_back(Edge(b, a, c, G[a].size() - 1)); } int dfs(vector<vector<Edge> >& G, int u, int t, int f, vector<bool>& used) { if (u == t) return f; used[u] = true; for (__typeof((G[u]).begin()) e = (G[u]).begin(); e != (G[u]).end(); ++e) if (e->cap > 0 && !used[e->dst]) { int g = dfs(G, e->dst, t, min(f, e->cap), used); if (g > 0) { e->cap -= g; G[e->dst][e->rev].cap += g; return g; } } return 0; } int max_flow(vector<vector<Edge> > G, int s, int t) { int N = G.size(); int flow = 0; while (true) { vector<bool> used(N, false); int f = dfs(G, s, t, INF, used); if (f == 0) return flow; flow += f; } } int main() { int N, M; while (cin >> N >> M && N) { vector<vector<Edge> > G(N); int minus = 0; for (int i = (0); i < (int)(M); ++i) { int a, b, c; cin >> a >> b >> c; if (c <= 0) { minus += c; continue; } add_edge(G, a, b, c); } int ans = INF; for (int s = (0); s < (int)(N); ++s) for (int t = (0); t < (int)(N); ++t) if (s != t) { ans = min(ans, max_flow(G, s, t)); } cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int maz[150][150]; int n, m; int stoer_wagner(int n) { bool vis[150]; int node[150], dis[150]; int maxj, prev; int ans = 1 << 30; memset(dis, 0, sizeof(dis)); for (int i = 0; i < n; i++) node[i] = i; while (n > 1) { prev = 0; maxj = 1; memset(vis, false, sizeof(vis)); vis[node[0]] = true; for (int i = 1; i < n; i++) { dis[node[i]] = maz[node[0]][node[i]]; if (dis[node[i]] > dis[node[maxj]]) maxj = i; } for (int i = 1; i < n; i++) { if (i == n - 1) { ans = (dis[node[maxj]] < ans ? dis[node[maxj]] : ans); for (int k = 0; k < n; k++) { maz[node[k]][node[prev]] += maz[node[k]][node[maxj]]; maz[node[prev]][node[k]] = maz[node[k]][node[prev]]; } node[maxj] = node[--n]; } prev = maxj; vis[node[maxj]] = true; maxj = -1; for (int k = 1; k < n; k++) { if (!vis[node[k]]) { dis[node[k]] += maz[node[prev]][node[k]]; if (maxj == -1 || dis[node[k]] > dis[node[maxj]]) maxj = k; } } } } return ans; } int main() { while (~scanf("%d%d", &n, &m)) { if (n == 0 || m == 0) break; memset(maz, 0, sizeof(maz)); int a, b, c, res = 0; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if (c <= res) { res += -c; continue; } maz[a][b] += c; maz[b][a] += c; } printf("%d\n", stoer_wagner(n) + res); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; struct Dinic { struct edge { int to, cap, rev; edge() {} edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {} }; int n; vector<vector<edge> > G; vector<map<int, int> > M; vector<int> level, iter; Dinic() {} Dinic(int sz) : n(sz), G(n), M(n), level(n), iter(n) {} void add_edge(int from, int to, int cap) { M[from][to] = G[from].size(); M[to][from] = G[to].size(); G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int flow(int s, int t, int lim) { int fl = 0; while (1) { bfs(s); if (level[t] < 0 || lim == 0) return fl; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, lim)) > 0) { fl += f; lim -= f; } } } int flow(int s, int t) { return flow(s, t, INF); } bool back_edge(int s, int t, int from, int to) { for (int i = 0; i < (int)G[from].size(); i++) { edge &e = G[from][i]; if (e.to == to) { if (e.cap == 0 && flow(from, to, 1) == 0) { flow(from, s, 1); flow(t, to, 1); return 1; } } } return 0; } }; int main() { int V, E; while (cin >> V >> E, V | E) { Dinic dinic(V); int minus = 0; for (int i = 0; i < E; i++) { int u, v, c; cin >> u >> v >> c; if (c <= 0) minus += c; else dinic.add_edge(u, v, c); } int ans = INF; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) continue; int f = dinic.flow(i, j); if (f != 0) ans = min(ans, f); dinic.flow(i, j); } } if (ans == INF) cout << 0 + minus << endl; else cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
python3
while True: n,m=map(int,input().split()) if (n|m)==0: break d={} for i in range(m): u,v,w=map(int,input().split()) if u>v: u,v=v,u d[(u,v)]=d.get((u,v),0)+w print(min(d.values()) if len(d) else 0)
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0, mn = 1234567890123456789LL, mn1 = 1234567890321456789LL; vector<pair<long long, pair<int, long long> > > edge[n]; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c <= 0) sum += c; else { if (c <= mn) { mn1 = mn; mn = c; } edge[x].push_back(make_pair(y, make_pair(i, c))); edge[y].push_back(make_pair(x, make_pair(i, c))); } } long long res = mn + mn1; { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); if (can[i][now]) continue; can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { pos.push(edge[now][j].first); } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { cout << sum << endl; goto next; } } for (int iii = 0; iii < int(n); ++iii) for (int jjj = 0; jjj < int(edge[iii].size()); ++jjj) { bool can[111][111] = {}; int num = edge[iii][jjj].second.first; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); if (can[i][now]) continue; can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (edge[now][j].second.first == num) continue; pos.push(edge[now][j].first); } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { res = min(res, edge[iii][jjj].second.second); } } cout << sum + res << endl; next:; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; int N, M; vector<Edge> G[110], cycle; int color[110]; void dfs(int v, vector<Edge> u) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { Edge &e = G[v][i]; int to = e.to; if (color[to] == -1) { u.push_back(G[v][i]); dfs(to, u); u.pop_back(); } else if (color[to] == 0) { color[to] = 1; u.push_back(G[v][i]); cycle = u; u.pop_back(); throw 0; } } color[v] = 1; } vector<vector<Edge>> cycles; bool solve() { vector<Edge> vec; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { try { if (color[i] == -1) { dfs(i, vec); } } catch (...) { cycles.push_back(cycle); cycle.clear(); } vec.clear(); } return (cycles.size() > 0); } void init() { cycle.clear(); cycles.clear(); for (int i = 0; i < 110; i++) { G[i].clear(); } } int main() { int s, t, cost; while (cin >> N >> M, N) { int min_cost = INT_MAX; init(); for (int i = 0; i < M; i++) { cin >> s >> t >> cost; G[s].push_back(Edge(s, t, cost)); min_cost = min(min_cost, cost); } if (solve()) { set<pair<int, int>> st; vector<int> v; if (cycles.size() == 1) { for (int i = 0; i < (int)cycles[0].size(); i++) { v.push_back(cycles[0][i].cost); st.insert(pair<int, int>(cycles[0][i].from, cycles[0][i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); i++) { Edge &e = G[i][j]; if (st.count(pair<int, int>(e.from, e.to)) == 0) { min_cost = min(min_cost, e.cost + min(0, v[0])); } } } } else { min_cost = 0; for (int i = 0; i < (int)cycles.size(); i++) { vector<int> v; for (int j = 0; j < (int)cycles[i].size(); j++) { v.push_back(cycles[i][j].cost); } sort(v.begin(), v.end()); if (v[0] < 0) { min_cost += v[0]; } } } } cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m; int x[100], y[100], c[100]; bool use[100]; bool union_find(void) { int i, j; int tmp, tmp2; int g[100]; for (i = 0; i < n; i++) g[i] = i; for (i = 0; i < m; i++) { if (g[x[i]] != g[y[i]] && !use[i]) { tmp = g[x[i]]; for (j = 0; j < n; j++) { if (g[j] == tmp) g[j] = g[y[i]]; } } } tmp = g[0]; tmp2 = -1; for (i = 1; i < n; i++) { if (tmp != g[i]) { if (tmp2 < 0) tmp2 = g[i]; else if (tmp2 != g[i]) return false; } } if (tmp2 < 0) return false; return true; } int main() { int cost; int i, j; while (1) { cin >> n >> m; if (!n && !m) break; for (i = 0; i < m; i++) { cin >> x[i] >> y[i] >> c[i]; use[m] = false; } if (union_find()) { cost = 0; cout << "no reduce" << endl; } else cost = 2147483647; for (i = 0; i < m; i++) { for (j = 0; j < m; j++) { use[i] = true; use[j] = true; if (union_find()) { if (i == j) { if (cost > c[i]) cost = c[i]; } else { if (cost > c[i] + c[j]) cost = c[i] + c[j]; } } use[i] = false; use[j] = false; } } cout << cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int next_flow = min(flow, e.flow); int d = dfs(e.to, next_flow, sink); if (d <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= next_flow; rev_e.flow += next_flow; return d; } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, flow)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; class UnionFindTree { private: int* par; int* rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } int res = numeric_limits<int>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; int N, M; vector<Edge> G[110], cycle; int color[110]; void dfs(int v, vector<Edge> u) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { Edge &e = G[v][i]; int to = e.to; if (color[to] == -1) { u.push_back(G[v][i]); dfs(to, u); u.pop_back(); } else if (color[to] == 0) { color[to] = 1; u.push_back(G[v][i]); cycle = u; u.pop_back(); throw 0; } } color[v] = 1; } vector<vector<Edge>> cycles; bool solve() { vector<Edge> vec; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { try { if (color[i] == -1) { dfs(i, vec); } } catch (...) { cycles.push_back(cycle); cycle.clear(); } vec.clear(); } return (cycles.size() > 0); } void init() { cycle.clear(); cycles.clear(); for (int i = 0; i < 110; i++) { G[i].clear(); } } class Union_Find { public: int par[110], rnk[110], size[110], gnum; Union_Find(int N) { gnum = N; for (int i = 0; i < N; i++) { par[i] = i; rnk[i] = 0; size[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; size[y] += size[x]; } else { par[y] = x; size[x] += size[y]; if (rnk[x] == rnk[y]) { rnk[x]++; } } gnum--; } bool same(int x, int y) { return (find(x) == find(y)); } int getSize(int x) { return size[find(x)]; } int groups() { return gnum; } }; int main() { int s, t, cost; while (cin >> N >> M, N) { int min_cost = INT_MAX; Union_Find uf(N); init(); for (int i = 0; i < M; i++) { cin >> s >> t >> cost; G[s].push_back(Edge(s, t, cost)); min_cost = min(min_cost, cost); uf.unite(s, t); } if (solve()) { set<pair<int, int>> st; vector<int> v; if (cycles.size() == 1) { for (int i = 0; i < (int)cycles[0].size(); i++) { v.push_back(cycles[0][i].cost); st.insert(pair<int, int>(cycles[0][i].from, cycles[0][i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); i++) { Edge &e = G[i][j]; if (st.count(pair<int, int>(e.from, e.to)) == 0) { min_cost = min(min_cost, e.cost + min(0, v[0])); } } } } else { min_cost = 0; } } else { if (uf.gnum != 1) { min_cost = 0; } } cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int x[100], y[100], c[100]; int r[100], use[100], tmp1, tmp2; int cost, total; int i, j; while (1) { cin >> n >> m; if (!n && !m) break; total = 0; for (i = 0; i < m; i++) { cin >> x[i] >> y[i] >> c[i]; total += c[i]; } for (i = 0; i < n; i++) { r[i] = i; use[i] = 0; } for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (c[i] < c[j]) { swap(x[i], x[j]); swap(y[i], y[j]); swap(c[i], c[j]); } } } cost = 0; for (i = 0; i < m; i++) { tmp1 = r[0]; tmp2 = -1; for (j = 1; j < n; j++) { if (tmp1 != r[j]) { if (tmp2 < 0) tmp2 = r[j]; else if (tmp2 != r[j]) break; } } if (j == n) break; if (c[i] >= 0 || r[x[i]] != r[y[i]]) { cost += c[i]; use[i] = 1; } tmp1 = r[y[i]]; for (j = 0; j < n; j++) { if (r[j] == tmp1) r[j] = r[x[i]]; } } for (i = 0; i < m; i++) { if (!use[i] && cost > 0 && r[x[i]] == r[y[i]]) cost += c[i]; i++; } cout << total - cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; long long n, m; long long mini; long long f[100]; pair<long long, pair<long long, long long> > e[100]; long long I, J; int main() { long long sum, cnt, x, y; while (cin >> n >> m && n && m) { cnt = sum = 0; for (int i = 0; i < m; i++) { cin >> e[i].second.first >> e[i].second.second >> e[i].first; sum += e[i].first; } cnt = sum; sort(e, e + m); reverse(e, e + m); mini = LONG_LONG_MAX; for (I = 0; I < n - 1; I++) { for (J = I + 1; J < n; J++) { sum = cnt; for (int i = 0; i < n; i++) f[i] = 0; f[I] = 1; f[J] = 2; for (int i = 0; i < m; i++) { if (f[e[i].second.first] + f[e[i].second.second] == 3) continue; sum -= e[i].first; f[e[i].second.first] = max(f[e[i].second.first], f[e[i].second.second]); f[e[i].second.second] = max(f[e[i].second.first], f[e[i].second.second]); } if (mini > sum) mini = sum; } } cout << mini << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int maz[110][110]; int n, m; int stoer_wagner(int n) { bool vis[110]; int node[110], dis[110]; int maxj, prev; int ans = 1 << 30; memset(dis, 0, sizeof(dis)); for (int i = 0; i < n; i++) node[i] = i; while (n > 1) { prev = 0; maxj = 1; memset(vis, false, sizeof(vis)); vis[node[0]] = true; for (int i = 1; i < n; i++) { dis[node[i]] = maz[node[0]][node[i]]; if (dis[node[i]] > dis[node[maxj]]) maxj = i; } for (int i = 1; i < n; i++) { if (i == n - 1) { ans = (dis[node[maxj]] < ans ? dis[node[maxj]] : ans); for (int k = 0; k < n; k++) { maz[node[k]][node[prev]] += maz[node[k]][node[maxj]]; maz[node[prev]][node[k]] = maz[node[k]][node[prev]]; } node[maxj] = node[--n]; } prev = maxj; vis[node[maxj]] = true; maxj = -1; for (int k = 1; k < n; k++) { if (!vis[node[k]]) { dis[node[k]] += maz[node[prev]][node[k]]; if (maxj == -1 || dis[node[k]] > dis[node[maxj]]) maxj = k; } } } } return ans; } int main() { while (~scanf("%d%d", &n, &m)) { if (n == 0 && m == 0) break; memset(maz, 0, sizeof(maz)); int a, b, c, res = 0; for (int i = 0; i < m; i++) { scanf("%d%d%d", &a, &b, &c); if (c < 0) { res += -c; continue; } maz[a][b] += c; maz[b][a] += c; } printf("%d\n", stoer_wagner(n) + res); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <sstream> #include <cstdlib> #include <cmath> #include <map> #include <set> #include <queue> #include <string> #include <vector> #include <algorithm> #include <functional> #include <cstdio> #include <numeric> #include <bitset> #include <stack> using namespace std; typedef struct { int to, cap, rev; } Edge; const int INF = 1 << 30; void add_edge(vector<vector<Edge> >& G, int from, int to, int cap) { Edge e1 = { to, cap, G[to].size() }; G[from].push_back(e1); Edge e2 = { from, 0, G[from].size()-1 }; G[to].push_back(e2); } int solve(vector<vector<Edge> >& G, vector<int>& used, int s, int t, int f) { if (s == t) return f; used[s] = 1; for (unsigned int i = 0; i < G[s].size(); ++i) { Edge& e = G[s][i]; if (!used[e.to] && e.cap > 0) { int d = solve(G, used, e.to, t, min(e.cap, f)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(vector<vector<Edge> > G, int s, int t) { int flow = 0; for ( ; ; ) { vector<int> used(G.size(), 0); int f = solve(G, used, s, t, INF); if (f == 0) return flow; flow += f; } } int main() { int n, m; while (~scanf("%d %d", &n, &m)) { if (n == 0 && m == 0) break; vector<vector<Edge> > G(n); int rev = 0; for (int i = 0; i < m; ++i) { int x, y, c; scanf("%d %d %d", &x, &y, &c); if (c < 0) { rev += c; c = 0; } add_edge(G, x, y, c); } int ans = INF; for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { ans = min(ans, max_flow(G, i, j)); } } printf("%d\n", ans+rev); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0; vector<pair<long long, long long> > edge[n]; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c <= 0) sum += c; else edge[x].push_back(make_pair(y, c)); } long long res = 1234567890123456789; { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (!can[i][edge[now][j].first]) { pos.push(edge[now][j].first); } } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { cout << sum << endl; goto next; } } for (int iii = 0; iii < int(n); ++iii) for (int jjj = 0; jjj < int(edge[iii].size()); ++jjj) for (int kkk = 0; kkk < int(n); ++kkk) for (int lll = 0; lll < int(edge[kkk].size()); ++lll) { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (now == iii && j == jjj) continue; if (now == kkk && j == lll) continue; if (!can[i][edge[now][j].first]) { pos.push(edge[now][j].first); } } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { if (iii == kkk && jjj == lll) { res = min(res, edge[iii][jjj].second); } else { res = min(res, edge[iii][jjj].second + edge[iii][jjj].second); } } } cout << sum + res << endl; next:; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int n, m; int ve[1200], nx[1200]; int st[1200]; long long re[1200], t[1200]; void add(int i, int a, int b, long long c) { ve[i] = b; re[i] = c; nx[i] = st[a]; st[a] = i; t[i] = 1; } bool visited[1200]; void dfs(int v) { if (visited[v]) return; visited[v] = 1; for (int i = st[v]; i; i = nx[i]) if (t[i]) dfs(ve[i]); } int check() { int cnt0 = 0; for (int v0 = 1; v0 <= n; v0++) { for (int k = 1; k <= n; k++) visited[k] = 0; dfs(v0); int flag0 = 0; for (int k = 1; k <= n; k++) if (!visited[k]) flag0 = 1; if (flag0) cnt0++; } if (cnt0 == n) return 1; else return 0; } int tot; int main() { scanf("%d%d", &n, &m); while (n + m != 0) { for (int k = 1; k <= n; k++) st[k] = 0; ; tot = 0; long long ans0 = 0; int anscnt = 0; for (int k = 1; k <= m; k++) { int x, y; long long z; scanf("%d %d %lld", &x, &y, &z); x++, y++; if (z <= 0) ans0 += z, anscnt++; else add(++tot, x, y, z); } if (check()) { printf("%lld\n", ans0); scanf("%d%d", &n, &m); continue; } long long ans = 1000000000; ans = ans * ans; long long mi1 = ans, mi2 = ans; for (int k = 1; k <= tot; k++) { t[k] = 0; if (check()) ans = min(ans, re[k]); t[k] = 1; if (re[k] < mi1) mi2 = mi1, mi1 = re[k]; else if (re[k] > mi2) mi2 = re[k]; } ans = min(ans, mi1 + mi2); ans += ans0; printf("%lld\n", ans); scanf("%d%d", &n, &m); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const long long inf = 1000000000000LL; using namespace std; struct edge { int u, v, next, pre; long long f; } e[100000]; int num, rnum; int n, m, N; int source, sink; int head[132], rhead[132], start[132]; int d[132], numb[132]; int p[132]; void Init() { memset(head, -1, sizeof(head)); memset(rhead, -1, sizeof(rhead)); memset(p, -1, sizeof(p)); num = 0; } void BFS() { int i, j; for (i = 1; i <= N; i++) { d[i] = N; numb[i] = 0; } int Q[132], head(0), tail(0); d[sink] = 0; numb[0] = 1; Q[++tail] = sink; while (head < tail) { i = Q[++head]; for (j = rhead[i]; j != -1; j = e[j].pre) { if (e[j].f == 0 || d[e[j].u] < N) continue; d[e[j].u] = d[i] + 1; numb[d[e[j].u]]++; Q[++tail] = e[j].u; } } } long long Augment() { int i; long long tmp = inf; for (i = p[sink]; i != -1; i = p[e[i].u]) { if (tmp > e[i].f) tmp = e[i].f; } for (i = p[sink]; i != -1; i = p[e[i].u]) { e[i].f -= tmp; e[i ^ 1].f += tmp; } return tmp; } int Retreat(int &i) { int tmp, j, mind(N - 1); for (j = head[i]; j != -1; j = e[j].next) { if (e[j].f > 0 && d[e[j].v] < mind) mind = d[e[j].v]; } tmp = d[i]; d[i] = mind + 1; numb[tmp]--; numb[d[i]]++; if (i != source) i = e[p[i]].u; return numb[tmp]; } long long maxflow() { long long flow(0); int i, j; BFS(); for (i = 1; i <= N; i++) start[i] = head[i]; i = source; while (d[source] < N) { for (j = start[i]; j != -1; j = e[j].next) if (e[j].f > 0 && d[i] == d[e[j].v] + 1) break; if (j != -1) { start[i] = j; p[e[j].v] = j; i = e[j].v; if (i == sink) { flow += Augment(); i = source; } } else { start[i] = head[i]; if (Retreat(i) == 0) break; } } return flow; } void addedge(int a, int b, long long c) { e[num].f = c; e[num].u = a; e[num].v = b; e[num].next = head[a]; head[a] = num; e[num].pre = rhead[b]; rhead[b] = num; num++; e[num].u = b; e[num].v = a; e[num].f = 0; e[num].next = head[b]; head[b] = num; e[num].pre = rhead[a]; rhead[a] = num; num++; } long long g[132][132]; inline long long min(long long a, long long b) { return a < b ? a : b; } int main() { int a, b; long long c; while (scanf("%d%d", &n, &m) != EOF) { if (n == 0 && m == 0) break; N = n; memset(g, 0, sizeof(g)); long long ans = 0, tmp = 0; for (int i = 0; i < m; i++) { scanf("%d %d %lld", &a, &b, &c); a++; b++; ans += c; if (c > 0) g[a][b] += c; else tmp += c; } for (int i = 2; i <= n; i++) { source = 1; sink = i; Init(); long long tt; for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) { if (j == k) continue; if (g[j][k] > 0) addedge(j, k, g[j][k]); } tt = maxflow(); source = i; sink = 1; Init(); for (int j = 1; j <= n; j++) for (int k = 1; k <= n; k++) { if (j == k) continue; if (g[j][k] > 0) addedge(j, k, g[j][k]); } ans = min(ans, tt + maxflow() + tmp); } printf("%lld\n", ans); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline bool minimize(T1& a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1& a, T2 b) { return a < b && (a = b, 1); } int const inf = 1 << 29; struct unicycle_graph { int N; using graph_type = pair<int, long long>; vector<vector<graph_type>> g; vector<int> cycle_vs_; vector<bool> in_cycle_v_; using edge = pair<int, int>; set<edge> in_cycle_edge_; unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {} void add_edge(int f, int t, int c) { g[f].emplace_back(t, c); g[t].emplace_back(f, c); } void build() { vector<bool> vis(N); vector<int> prev(N); std::function<bool(int, int)> dfs = [&](int x, int p) { for (auto const& e : g[x]) { int to, cost; tie(to, cost) = e; if (to == p) { continue; } prev[to] = x; if (vis[to]) { for (int curr = x; curr != to; curr = prev[curr]) { cycle_vs_.push_back(curr); in_cycle_v_[curr] = 1; in_cycle_edge_.emplace(prev[curr], curr); in_cycle_edge_.emplace(curr, prev[curr]); } cycle_vs_.push_back(to); in_cycle_v_[to] = 1; in_cycle_edge_.emplace(to, prev[to]); in_cycle_edge_.emplace(prev[to], to); reverse((cycle_vs_).begin(), (cycle_vs_).end()); return true; } else { vis[to] = 1; if (dfs(to, x)) { return true; } } } return false; }; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; if (dfs(i, -1)) { break; } } } } bool in_cycle(int x) { return in_cycle_v_[x]; } bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); } vector<int> const& cycle_vertices() { return cycle_vs_; } }; struct union_find { vector<int> rank_, size_, rid_; union_find(int n) { rank_.resize(n); rid_.assign(n, -1); size_.resize(n, 1); } void operator()(int u, int v) { u = operator[](u), v = operator[](v); if (u == v) { return; } size_[u] = size_[v] = size_[u] + size_[v]; if (rank_[u] < rank_[v]) { rid_[u] = v; } else { rid_[v] = u; if (rank_[u] == rank_[v]) { rank_[u]++; } } } int operator[](int x) { if (rid_[x] < 0) return x; else return rid_[x] = operator[](rid_[x]); } int size_of(int x) { return size_[x]; } }; int main() { for (int N, M; cin >> N >> M && (N | M);) { unicycle_graph ug(N); union_find uf(N); vector<pair<long long, unicycle_graph::edge>> all_edges; for (int i = 0; i < (int)M; i++) { int x, y; long long c; cin >> x >> y >> c; all_edges.emplace_back(c, make_pair(x, y)); ug.add_edge(x, y, c); uf(x, y); } ug.build(); sort((all_edges).begin(), (all_edges).end()); vector<long long> ccosts; long long mcost = 1e10; for (int i = 0; i < (int)M; i++) { auto c = all_edges[i].first; auto e = all_edges[i].second; if (ug.in_cycle_edge(e.first, e.second)) { ccosts.push_back(c); } else { minimize(mcost, c); } } if (ccosts.size() > 1) minimize(mcost, ccosts[0] + ccosts[1]); if (mcost == 1e10) cout << 0 << endl; else cout << mcost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; int next_flow = min(flow, e.flow); if (next_flow == 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= next_flow; rev_e.flow += next_flow; res = max(res, dfs(e.to, next_flow, sink)); } return res; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(0, house_i)); } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1107110711071107; vector<pair<long long, long long> > G[101]; long long min_bridge = INF; long long ord[101], low[101]; bool vis[101]; void dfs(long long v, long long p, long long &k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; for (long long i = 0; i < (G[v].size()); i++) { if (!vis[G[v][i].first]) { dfs(G[v][i].first, v, k); low[v] = min(low[v], low[G[v][i].first]); if (ord[v] < low[G[v][i].first]) min_bridge = min(min_bridge, G[v][i].second); } else if (G[v][i].first != p) { low[v] = min(low[v], ord[G[v][i].first]); } } } signed main() { long long n, m; while (cin >> n >> m, n | m) { long long minus_cost = 0; min_bridge = INF; long long min_1 = INF, min_2 = INF; for (long long i = 0; i < (101); i++) { G[i].clear(); vis[i] = false; } for (long long i = 0; i < (m); i++) { long long a, b, c; cin >> a >> b >> c; if (c <= 0) minus_cost += c; else { if (min_1 > c) min_2 = min_1, min_1 = c; else if (min_2 > c) min_2 = c; G[a].push_back(pair<long long, long long>(b, c)); } } long long k = 0; for (long long i = 0; i < (n); i++) { if (!vis[i]) dfs(i, -1, k); } if (min_bridge == INF) cout << minus_cost << endl; else cout << min(min_bridge, min_1 + min_2) + minus_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline bool minimize(T1& a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1& a, T2 b) { return a < b && (a = b, 1); } int const inf = 1 << 29; struct unicycle_graph { int N; using graph_edge_t = pair<int, long long>; vector<vector<graph_edge_t>> g; vector<int> cycle_vs_; vector<bool> in_cycle_v_; using edge = pair<int, int>; set<edge> in_cycle_edge_; unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {} void add_edge(int f, int t, int c) { g[f].emplace_back(t, c); g[t].emplace_back(f, c); } bool remove_edge(int f, int t) { for (int i = 0; i < (int)g[f].size(); i++) { if (g[f][i].first == t) { g[f].erase(g[f].begin() + i); for (int k = 0; k < (int)g[t].size(); k++) { if (g[t][k].first == f) { g[t].erase(g[t].begin() + k); if (in_cycle_edge(f, t)) { cycle_vs_.clear(); in_cycle_v_.clear(); in_cycle_v_.resize(N); in_cycle_edge_.clear(); } return true; } } } } return false; } void build() { vector<bool> vis(N); vector<int> prev(N); std::function<bool(int, int)> dfs = [&](int x, int p) { for (auto const& e : g[x]) { int to, cost; tie(to, cost) = e; if (to == p) { continue; } prev[to] = x; if (vis[to]) { for (int curr = x; curr != to; curr = prev[curr]) { cycle_vs_.push_back(curr); in_cycle_v_[curr] = 1; in_cycle_edge_.emplace(prev[curr], curr); in_cycle_edge_.emplace(curr, prev[curr]); } cycle_vs_.push_back(to); in_cycle_v_[to] = 1; in_cycle_edge_.emplace(to, prev[to]); in_cycle_edge_.emplace(prev[to], to); reverse((cycle_vs_).begin(), (cycle_vs_).end()); return true; } else { vis[to] = 1; if (dfs(to, x)) { return true; } } } return false; }; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; if (dfs(i, -1)) { break; } } } } bool in_cycle(int x) { return in_cycle_v_[x]; } bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); } vector<int> const& cycle_vertices() { return cycle_vs_; } }; int count_components(unicycle_graph::vector<vector<graph_edge_t>>& g) { int N = g.size(); vector<bool> vis(N); std::function<void(int)> dfs = [&](int x) { for (auto&& e : g[x]) { if (vis[e.first]) { continue; } vis[e.first] = 1; dfs(e.first); } }; int ret = 0; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; dfs(i); ret++; } } return ret; } int main() { for (int N, M; cin >> N >> M && (N | M);) { unicycle_graph ug(N); vector<pair<long long, unicycle_graph::edge>> all_edges; for (int i = 0; i < (int)M; i++) { int x, y; long long c; cin >> x >> y >> c; all_edges.emplace_back(c, make_pair(x, y)); ug.add_edge(x, y, c); } ug.build(); sort((all_edges).begin(), (all_edges).end()); vector<long long> ccosts; long long mcost = 1e10; for (int i = 0; i < (int)M; i++) { auto c = all_edges[i].first; auto e = all_edges[i].second; if (ug.in_cycle_edge(e.first, e.second)) { ccosts.push_back(c); } else { minimize(mcost, c); } } if (ccosts.size() > 1) mcost = min({mcost, ccosts[0] + ccosts[1], mcost + ccosts[0]}); if (mcost == 1e10) cout << 0 << endl; else cout << mcost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); FordFulkerson ff2(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); ff2.add_edge(to, from, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); int lhs = tmp.compute_maxflow(0, house_i); FordFulkerson tmp2(ff2); int rhs = tmp2.compute_maxflow(0, house_i); res = min(res, max(lhs, rhs)); } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline bool minimize(T1& a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1& a, T2 b) { return a < b && (a = b, 1); } int const inf = 1 << 29; struct unicycle_graph { int N; using graph_type = pair<int, long long>; vector<vector<graph_type>> g; vector<int> cycle_vs_; vector<bool> in_cycle_v_; using edge = pair<int, int>; set<edge> in_cycle_edge_; map<edge, long long> costs_; unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {} void add_edge(int f, int t, int c) { g[f].emplace_back(t, c); g[t].emplace_back(f, c); costs_[{f, t}] = c; costs_[{t, f}] = c; } void build() { vector<bool> vis(N); vector<int> prev(N); std::function<bool(int, int)> dfs = [&](int x, int p) { for (auto const& e : g[x]) { int to, cost; tie(to, cost) = e; if (to == p) { continue; } prev[to] = x; if (vis[to]) { for (int curr = x; curr != to; curr = prev[curr]) { cycle_vs_.push_back(curr); in_cycle_v_[curr] = 1; in_cycle_edge_.emplace(prev[curr], curr); in_cycle_edge_.emplace(curr, prev[curr]); } cycle_vs_.push_back(to); in_cycle_v_[to] = 1; in_cycle_edge_.emplace(to, prev[to]); in_cycle_edge_.emplace(prev[to], to); reverse((cycle_vs_).begin(), (cycle_vs_).end()); return true; } else { vis[to] = 1; if (dfs(to, x)) { return true; } } } return false; }; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; if (dfs(i, -1)) { break; } } } } bool in_cycle(int x) { return in_cycle_v_[x]; } bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); } vector<int> const& cycle_vertices() { return cycle_vs_; } }; int main() { for (int N, M; cin >> N >> M && (N | M);) { unicycle_graph ug(N); vector<pair<long long, unicycle_graph::edge>> all_edges; for (int i = 0; i < (int)M; i++) { int x, y, c; cin >> x >> y >> c; all_edges.emplace_back(c, make_pair(x, y)); ug.add_edge(x, y, c); } ug.build(); sort((all_edges).begin(), (all_edges).end()); vector<long long> ccosts; long long mcost = 1e10; for (int i = 0; i < (int)M; i++) { auto c = all_edges[i].first; auto e = all_edges[i].second; if (ug.in_cycle_edge(e.first, e.second)) { ccosts.push_back(c); } else { minimize(mcost, c); } } if (ccosts.size() > 1) { cout << min(ccosts[0] + ccosts[1], mcost) << endl; } else { cout << mcost << endl; } } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; int next_flow = min(flow, e.flow); if (next_flow <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= next_flow; rev_e.flow += next_flow; res = max(res, dfs(e.to, next_flow, sink)); } return res; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, flow)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; class UnionFindTree { private: int* par; int* rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } int res = numeric_limits<int>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
#include <bits/stdc++.h> using namespace std; template <class T> int popcount(T n); template <> int popcount(unsigned int n) { return n ? __builtin_popcount(n) : 0; } template <> int popcount(int n) { return n ? __builtin_popcount(n) : 0; } template <> int popcount(unsigned long long n) { return n ? __builtin_popcountll(n) : 0; } template <> int popcount(long long n) { return n ? __builtin_popcountll(n) : 0; } template <class T> void max_swap(T& a, const T& b) { a = max(a, b); } template <class T> void min_swap(T& a, const T& b) { a = min(a, b); } const double EPS = 1e-8; const double PI = acos(-1.0); const int dx[] = {0, 1, 0, -1}; const int dy[] = {1, 0, -1, 0}; bool valid_pos(int x, int y, int w, int h) { return 0 <= x && x < w && 0 <= y && y < h; } template <class T> void print(T a, int n, int br = 1, const string& deli = ", ") { cout << "{ "; for (int i = 0; i < n; ++i) { cout << a[i]; if (i + 1 != n) cout << deli; } cout << " }"; while (br--) cout << endl; } template <class T> void print(const vector<T>& v, int br = 1, const string& deli = ", ") { print(v, v.size(), br, deli); } template <class T> class UnionFind { private: T* parent; T* rank; public: int groups; UnionFind(int n) : groups(n) { parent = new T[n]; rank = new T[n]; for (int i = 0; i < n; ++i) { parent[i] = i; rank[i] = 0; } } void unite(T a, T b) { T root_a = find(a); T root_b = find(b); if (root_a == root_b) return; --groups; if (rank[root_a] < rank[root_b]) parent[root_a] = root_b; else if (rank[root_a] > rank[root_b]) parent[root_b] = root_a; else { parent[root_a] = root_b; ++rank[root_a]; } } bool same(T a, T b) { return find(a) == find(b); } T find(T e) { if (parent[e] == e) return e; else return parent[e] = find(parent[e]); } }; class SCC { public: int V; vector<vector<int> > g; vector<vector<int> > rg; vector<int> vs; vector<bool> used; vector<int> component; SCC(int V) : V(V), g(V), rg(V), vs(V), used(V), component(V) {} void add_edge(int from, int to) { g[from].push_back(to); rg[to].push_back(from); } void dfs(int v) { used[v] = true; for (int i = 0; i < g[v].size(); ++i) if (!used[g[v][i]]) dfs(g[v][i]); vs.push_back(v); } void rdfs(int v, int k) { used[v] = true; component[v] = k; for (int i = 0; i < rg[v].size(); ++i) if (!used[rg[v][i]]) rdfs(rg[v][i], k); } int scc() { vs.clear(); fill(used.begin(), used.end(), false); for (int i = 0; i < V; ++i) if (!used[i]) dfs(i); fill(used.begin(), used.end(), false); int k = 0; for (int i = vs.size() - 1; i >= 0; --i) if (!used[vs[i]]) rdfs(vs[i], k++); return k; } }; int main() { ios::sync_with_stdio(false); int n, m; while (cin >> n >> m, n) { vector<pair<int, int> > e[128]; UnionFind<int> uf(n); int res = 0; while (m--) { int x, y, c; cin >> x >> y >> c; if (c <= 0) res += c; else { e[x].push_back(pair<int, int>(y, c)); uf.unite(x, y); } } if (uf.groups == 1) { SCC scc(n); for (int i = 0; i < n; ++i) for (int j = 0; j < e[i].size(); ++j) scc.add_edge(i, e[i][j].first); scc.scc(); int min_cost = INT_MAX; map<int, vector<int> > cost_in_scc; for (int i = 0; i < n; ++i) { for (int j = 0; j < e[i].size(); ++j) { int x = i, y = e[i][j].first, c = e[i][j].second; if (scc.component[x] != scc.component[y]) min_swap(min_cost, c); else cost_in_scc[scc.component[x]].push_back(c); } } for (__typeof__((cost_in_scc).begin()) it = (cost_in_scc).begin(); it != (cost_in_scc).end(); ++it) { vector<int>& v = it->second; sort((v).begin(), (v).end()); min_swap(min_cost, v[0] + v[1]); } res += min_cost; } cout << res << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long oo = 1LL << 60; const long long B = 1LL << 31; const long long W = 10000; const int Max = 128; const int MaxE = 500000; struct box { int to; long long f; box *s, *r; } edge[MaxE], *hd[Max], *cur[Max], *cp; box rem[MaxE]; int n, s, t, vH[Max], H[Max]; box *addEdge(int x, int y, long long f) { box *p = cp++, *q = cp++; p->to = y, p->s = hd[x], hd[x] = p; q->to = x, q->s = hd[y], hd[y] = q; p->f = f, q->f = 0; p->r = q, q->r = p; return p; } long long aug(int x, long long f) { if (x == t) return f; long long d; int minH = n - 1; for (box *p = cur[x]; p; p = p->s) if (p->f > 0 && H[x] == H[p->to] + 1) { cur[x] = p; if ((d = aug(p->to, f < p->f ? f : p->f)) > 0) { p->f -= d, p->r->f += d; return d; } if (H[s] >= n) return 0; } if (--vH[H[x]] == 0) H[s] = n; for (box *p = cur[x] = hd[x]; p; p = p->s) if (p->f > 0 && H[p->to] < minH) minH = H[p->to]; ++vH[H[x] = minH + 1]; return 0; } long long maxFlow() { memcpy(cur, hd, sizeof(cur[0]) * n); memset(vH, 0, sizeof(vH[0]) * (n + 1)); memset(H, 0, sizeof(H[0]) * n); long long res = 0, d; vH[0] = n; while (H[s] < n) if ((d = aug(s, oo * 2LL)) > 0) res += d; return res; } int color[Max]; int dfs(int x, int s) { int sz = 1; color[x] = s; for (box *p = hd[x]; p; p = p->s) if (color[p->to] != s && p->f > 0) sz += dfs(p->to, s); return sz; } long long cal(int N, int cn, int c) { if (cn == 1) return oo; int cs = -1, ct = -1; for (int i = 0; i < N && ct == -1; i++) if (color[i] == c) { if (cs == -1) cs = i; else if (ct == -1) ct = i; } n = N, s = cs, t = ct; memcpy(edge, rem, sizeof(rem[0]) * (cp - edge)); long long cm = maxFlow(); long long cf = cm / W, cd = cm % W; long long cr = cf - cd * B; int ns = dfs(cs, cs); for (int i = 0; i < N; i++) if (color[i] == c) color[i] = ct; cr = min(cr, cal(N, ns, cs)); cr = min(cr, cal(N, cn - ns, ct)); return cr; } long long mp[Max][Max]; int main() { for (int N, M; scanf("%d%d", &N, &M) && N;) { for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) mp[i][j] = oo; for (int i = 0; i < M; i++) { int x, y, c; scanf("%d%d%d", &x, &y, &c); if (mp[x][y] == oo) mp[x][y] = c; else mp[x][y] += c; } memset(hd, 0, sizeof(hd[0]) * N); cp = edge; for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) if (mp[i][j] < B) addEdge(i, j, (mp[i][j] + B) * W + 1); memcpy(rem, edge, sizeof(rem[0]) * (cp - edge)); memset(color, -1, sizeof(color[0]) * N); printf("%lld\n", cal(N, N, -1)); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(0, house_i)); } if (res == numeric_limits<int>::max()) res = 0; printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } int bsr(int x) { return 31 - __builtin_clz(x); } template <class E> using Graph = vector<vector<E>>; template <class D, D INF> struct BidirectedCut { D sum = INF; template <class E> BidirectedCut(Graph<E> g) { int n = (int)g.size(); int m_a = -1, m_b = -1; vector<D> dist_base(n, 0); for (int m = n; m > 1; m--) { int a, b; auto dist = dist_base; for (int i = 0; i < m; i++) { a = b; b = max_element(begin(dist), end(dist)) - begin(dist); if (i == m - 1) sum = min(sum, dist[b]); dist[b] = -1; for (E e : g[b]) { if (e.to == m_b) e.to = m_a; if (dist[e.to] == -1) continue; dist[e.to] += e.dist; } } m_a = a; m_b = b; g[a].insert(end(g[a]), begin(g[b]), end(g[b])); g[b].clear(); dist_base[b] = -1; } } }; bool solve() { int n, m; cin >> n >> m; if (!n) return false; struct Edge { int to; int dist; }; Graph<Edge> g(n); auto addEdge = [&](int a, int b, int c) { g[a].push_back(Edge{b, c}); g[b].push_back(Edge{a, c}); }; for (int i = 0; i < m; i++) { int a, b, c; cin >> a >> b >> c; addEdge(a, b, c); } cout << BidirectedCut<int, TEN(8)>(g).sum << endl; return true; } int main() { while (solve()) { } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long INF = 1107110711071107; vector<pair<long long, long long> > G[101]; long long min_bridge = INF; long long ord[101], low[101]; bool vis[101]; void dfs(long long v, long long p, long long &k) { vis[v] = true; ord[v] = k++; low[v] = ord[v]; for (long long i = 0; i < (G[v].size()); i++) { if (!vis[G[v][i].first]) { dfs(G[v][i].first, v, k); low[v] = min(low[v], low[G[v][i].first]); if (ord[v] < low[G[v][i].first]) min_bridge = min(min_bridge, G[v][i].second); } else if (G[v][i].first != p) { low[v] = min(low[v], ord[G[v][i].first]); } } } signed main() { long long n, m; while (cin >> n >> m, n | m) { long long minus_cost = 0; min_bridge = INF; long long min_1 = INF, min_2 = INF; for (long long i = 0; i < (101); i++) { G[i].clear(); vis[i] = false; } for (long long i = 0; i < (m); i++) { long long a, b, c; cin >> a >> b >> c; if (c <= 0) minus_cost += c; else { if (min_1 > c) min_2 = min_1, min_1 = c; else if (min_2 > c) min_2 = c; G[a].push_back(pair<long long, long long>(b, c)); G[b].push_back(pair<long long, long long>(a, c)); } } long long k = 0; for (long long i = 0; i < (n); i++) { if (!vis[i]) dfs(i, -1, k); } if (min_bridge == INF) cout << minus_cost << endl; else cout << min(min_bridge, min_1 + min_2) + minus_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; vector<Edge> G[110]; vector<Edge> used; int N, color[110]; void dfs(int v, vector<Edge> vec) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { int to = G[v][i].to, cost = G[v][i].cost; if (color[to] == -1) { vec.push_back(Edge(i, to, cost)); dfs(to, vec); vec.pop_back(); } else if (color[to] == 0) { vec.push_back(Edge(i, to, cost)); used = vec; return; } } color[v] = 1; } bool solve() { vector<Edge> vec; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { if (color[i] == -1) { dfs(i, vec); } } return (used.size() > 0); } void init() { for (int i = 0; i < 110; i++) { G[i].clear(); } } int main() { int N, M, a, b, c; while (cin >> N >> M, N) { init(); int min_cost = (1 << 29); for (int i = 0; i < M; i++) { cin >> a >> b >> c; G[a].push_back(Edge(a, b, c)); min_cost = min(min_cost, c); } if (solve()) { vector<int> v; set<pair<int, int> > st; for (int i = 0; i < (int)used.size(); i++) { v.push_back(used[i].cost); st.insert(pair<int, int>(used[i].from, used[i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); j++) { int to = G[i][j].to; if (st.count(pair<int, int>(i, to)) == 0) { min_cost = min(min_cost, G[i][j].cost); } } } } cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<algorithm> #include<vector> #include<climits> #define MAX_N 100 using namespace std; typedef pair<int,int> P; typedef pair<int, P > PP; int n,m; int par[MAX_N]; int pa[MAX_N]; int rank[MAX_N]; int mini; void init(int); int find(int); void unite(int x,int y); bool same(int,int); PP e[MAX_N]; int I,J; int main(){ int sum,cnt,x,y; while(cin>>n>>m&&n&&m){ cnt=sum=0; for(int i=0;i<m;i++){ cin>>e[i].second.first>>e[i].second.second>>e[i].first; sum+=e[i].first; } cnt=sum; sort(e,e+m); reverse(e,e+m); mini=INT_MAX; for(I=0;I<n-1;I++){ for(J=I+1;J<n;J++){ sum=cnt; init(n); for(int i=0;i<m;i++){ x=find(e[i].second.first); y=find(e[i].second.second); if(pa[x]+pa[y]==3)continue; //cout<<"pa "<<pa[x]<<' '<<pa[y]<<endl; //cout<<"no "<<e[i].second.first<<' '<<e[i].second.second<<endl; sum-=e[i].first; unite(e[i].second.first,e[i].second.second); } //cout<<sum<<endl; if(mini>sum)mini=sum; } } cout<<mini<<endl; } } void init(int n){ for(int i=0;i<n;i++){ pa[i]=0; if(i==I)pa[i]=1; if(i==J)pa[i]=2; par[i]=i; rank[i]=0; } } int find (int x){ if(par[x]==x){ return x; }else{ return par[x]=find(par[x]); } } void unite(int x,int y){ x=find(x); y=find(y); if(x==y)return; if(rank[x]<rank[y]){ par[x]=y; pa[y]=max(pa[x],pa[y]); }else{ par[y]=x; pa[x]=max(pa[y],pa[x]); if(rank[x]==rank[y])rank[x]++; } } bool same(int x,int y){ return find(x)==find(y); }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0, mn = 1234567890123456789LL, mn1 = 1234567890123456789; vector<pair<int, int> > edge[n]; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c < 0) sum += c; if (mn >= c) { mn1 = mn; mn = c; } edge[x].push_back(make_pair(y, c)); } if (mn < 0) { cout << sum << endl; continue; } bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<int> pos; pos.push(i); while (!pos.empty()) { int now = pos.front(); pos.pop(); can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (!can[i][edge[now][j].first]) { pos.push(edge[now][j].first); } } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { cout << 0 << endl; goto next; } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j]) { cout << mn << endl; goto next; } cout << mn + mn1 << endl; next:; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
// AOJ 1065 The House of Huge Family // 2018.2.3 bal4u #include <stdio.h> #include <stdlib.h> #define MAX 102 /* UNION-FIND library */ int id[MAX], size[MAX]; void init(int n) { int i; for (i = 0; i < n; i++) id[i] = i, size[i] = 1; } int root(int i) { while (i != id[i]) id[i] = id[id[i]], i = id[i]; return i; } int connected(int p, int q) { return root(p) == root(q); } void unite(int p, int q) { int i = root(p), j = root(q); if (i == j) return; if (size[i] < size[j]) id[i] = j, size[j] += size[i]; else id[j] = i, size[i] += size[j]; } /* UNION-FIND library */ //#define getchar_unlocked() getchar() int in() { int n = 0; int c = getchar_unlocked(); if (c == '-') { c = getchar_unlocked(); do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked(); while (c >= '0'); return -n; } do n = (n<<3)+(n<<1) + (c & 0xf), c = getchar_unlocked(); while (c >= '0'); return n; } typedef struct { int x, y, c; } T; T tbl[MAX]; int sz; int cmp(T *a, T *b) { return a->c - b->c; } int main() { int n, m, i, ans; while (n = in()) { m = in(); init(n); ans = 0; for (sz = 0, i = 0; i < m; i++) { tbl[sz].x = in(), tbl[sz].y = in(), tbl[sz].c = in(); if (tbl[sz].c < 0) ans += tbl[sz].c; else sz++; } qsort(tbl, sz, sizeof(T), cmp); for (i = 0; i < sz; i++) { if (!connected(tbl[i].x, tbl[i].y)) { if (n > 2) unite(tbl[i].x, tbl[i].y), n--; else ans += tbl[i].c; } } printf("%d\n", ans); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
/* * sy.cpp * * Created on: 2012-10-7 * Author: Administrator */ #include<iostream> #include<algorithm> #include<cstdio> #include<cstring> #include<string> #include<cmath> #include<queue> using namespace std; typedef long long ll; #define zero(a) memset(a,0,sizeof(a)) const double pi = acos(-1.0); const int inf = 0x7FFFFFFF; const double eps = 1e-8; int n, m; const int N = 1005; const int M = 10005; int k[101][101]; bool f[101]; int tot; struct edge { int next, v; } e[M]; int head[N], index; void init() { zero(head); index = 1; } inline void add(int u, int v) { e[index].v = v; e[index].next = head[u]; head[u] = index++; } void bfs(int x) { f[x] = 1; tot++; int i, j; queue<int> q; q.push(x); while (!q.empty()) { int tmp = q.front(); q.pop(); for (i = head[tmp]; i; i = e[i].next) { if (!f[e[i].v]) { f[e[i].v] = 1; tot++; q.push(e[i].v); } } } } bool g[101],ac[101]; void jud(int x) { zero(f); zero(ac); f[x] = 1; queue<int> q; q.push(x); int i,j; while (!q.empty()) { int tmp = q.front(); q.pop(); for (i = head[tmp]; i; i = e[i].next) { if(tmp == x){ ac[e[i].v] = 1; } if(e[i].v == x && ac[tmp] == 0){ g[x] = 1;return; } if (!f[e[i].v]) { f[e[i].v] = 1; q.push(e[i].v); } } } } int m1[10000],m2[10000],t1,t2; int main() { int i, j, a, b, c, ans; while (cin >> n >> m && (n || m)) { for(i = 1;i <= n;i ++){ for(j = 1;j <= n; j++){ k[i][j] = -inf; } } zero(f); init(); ans = 0; for (i = 0; i < m; i++) { cin >> a >> b >> c; a++; b++; if (c <= 0) { ans += c; continue; } k[a][b] = max(0, k[a][b]); k[b][a] = max(0, k[b][a]); k[a][b] += c; k[b][a] += c; add(a, b); add(b, a); } tot = 0; bfs(1); if (tot != n) { cout << ans << endl; continue; }zero(g); for (i = 1; i <= n; i++) { jud(i); } t1 = t2 = 0; for(i = 1;i <= n;i ++){ for(j = i+1;j <= n; j++){ if(k[i][j] == -inf) continue; if(g[i]&&g[j]){ m1[t1++] = k[i][j]; } else m2[t2++] = k[i][j]; } } sort(m1,m1+t1); sort(m2,m2+t2); if(t1 > 1){ cout<<ans + min(m1[0]+m1[1],m2[0])<<endl; } else cout<<ans + m2[0]<<endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <string> #include <cmath> #include <cstdio> #include <cstring> #include <cstdlib> #include <ctime> #include <queue> #include <stack> #include <list> #include <algorithm> using namespace std; #define typec __int64 #define V 111 //const typec inf = 0x3f3f3f3f; const typec maxw = 111; typec g[V][V], w[V]; typec a[V], v[V], na[V]; typec Stoer_Wagner(int n) { int i, j, pv, zj; typec best = maxw * n * n; for (i = 0; i < n; i++) v[i] = i; // vertex: 0 ~ n-1 while (n > 1) { for (a[v[0]] = 1, i = 1; i < n; i++) { a[v[i]] = 0; na[i - 1] = i; w[i] = g[v[0]][v[i]]; } for (pv = v[0], i = 1; i < n; i++ ) { for (zj = -1, j = 1; j < n; j++ ) if (!a[v[j]] && (zj < 0 || w[j] > w[zj])) zj = j; a[v[zj]] = 1; if (i == n - 1) { if (best > w[zj]) best = w[zj]; for (i = 0; i < n; i++) g[v[i]][pv] = g[pv][v[i]] += g[v[zj]][v[i]]; v[zj] = v[--n]; break; } pv = v[zj]; for (j = 1; j < n; j++) if(!a[v[j]]) w[j] += g[v[zj]][v[j]]; } } return best; } int main() { //freopen("G:\\in.txt","r",stdin); int n, m, u, v, w; while(scanf("%d%d", &n, &m) != EOF,n+m) { memset(g, 0, sizeof(g)); while(m --) { scanf("%d%d%d", &u, &v, &w); g[u][v] += w; g[v][u] += w; } printf("%lld\n", Stoer_Wagner(n)); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; int block_cost[101] = {}; bool used[101]; memset(used, false, sizeof(used)); for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); block_cost[to] += cost; used[to] = true; } int res = 0x3f3f3f3f; for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!used[house_i]) continue; res = min(block_cost[house_i], res); } printf("%d\n", res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int x[100], y[100], c[100]; int r[100], tmp1, tmp2; int cost, total; int i, j; while (1) { cin >> n >> m; if (!n && !m) break; total = 0; for (i = 0; i < m; i++) { cin >> x[i] >> y[i] >> c[i]; total += c[i]; } for (i = 0; i < n; i++) r[i] = i; for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (c[i] < c[j]) { swap(x[i], x[j]); swap(y[i], y[j]); swap(c[i], c[j]); } } } cost = 0; for (i = 0; i < m; i++) { tmp1 = r[0]; tmp2 = -1; for (j = 1; j < n; j++) { if (tmp1 != r[j]) { if (tmp2 < 0) tmp2 = r[j]; else if (tmp2 != r[j]) break; } } if (j == n) break; if (r[x[i]] != r[y[i]]) { cost += c[i]; tmp1 = r[y[i]]; for (j = 0; j < n; j++) { if (r[j] == tmp1) r[j] = r[x[i]]; } } } i++; while (c[i] >= 0 && i < m) { if (r[x[i]] == r[y[i]]) cost += c[i]; i++; } cout << total - cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); FordFulkerson ff2(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); ff2.add_edge(to, from, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); int lhs = tmp.compute_maxflow(0, house_i); FordFulkerson tmp2(ff2); int rhs = tmp2.compute_maxflow(house_i, 0); res = min(res, max(lhs, rhs)); } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; struct Dinic { struct edge { int to, cap, rev; edge() {} edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {} }; int n; vector<vector<edge> > G; vector<map<int, int> > M; vector<int> level, iter; Dinic() {} Dinic(int sz) : n(sz), G(n), M(n), level(n), iter(n) {} void add_edge(int from, int to, int cap) { M[from][to] = G[from].size(); M[to][from] = G[to].size(); G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int flow(int s, int t, int lim) { int fl = 0; while (1) { bfs(s); if (level[t] < 0 || lim == 0) return fl; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, lim)) > 0) { fl += f; lim -= f; } } } int flow(int s, int t) { return flow(s, t, INF); } bool back_edge(int s, int t, int from, int to) { for (int i = 0; i < (int)G[from].size(); i++) { edge &e = G[from][i]; if (e.to == to) { if (e.cap == 0 && flow(from, to, 1) == 0) { flow(from, s, 1); flow(t, to, 1); return 1; } } } return 0; } }; int main() { int V, E; int minus = 0; while (cin >> V >> E, V | E) { Dinic dinic(V); for (int i = 0; i < E; i++) { int u, v, c; cin >> u >> v >> c; if (c <= 0) minus += c; else dinic.add_edge(u, v, c); } int ans = INF; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) continue; int f = dinic.flow(i, j); if (f != 0) ans = min(ans, f); } } if (ans == INF) cout << 0 + minus << endl; else cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0, mn = 1234567890123456789LL, mn1 = 1234567890321456789LL; vector<pair<long long, long long> > edge[n]; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c <= 0) sum += c; else { if (c <= mn) { mn1 = mn; mn = c; } edge[x].push_back(make_pair(y, c)); } } long long res = mn + mn1; { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); if (can[i][now]) continue; can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { pos.push(edge[now][j].first); } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { cout << sum << endl; goto next; } } for (int iii = 0; iii < int(n); ++iii) for (int jjj = 0; jjj < int(edge[iii].size()); ++jjj) { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); if (can[i][now]) continue; can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (now == iii && j == jjj) continue; pos.push(edge[now][j].first); } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { res = min(res, edge[iii][jjj].second); } } cout << sum + res << endl; next:; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int mat[103][103], maty[103][103]; int n, m, ans, ansf; void reset() { memcpy(mat, maty, sizeof(mat)); } int max_flow(int n, int mat[][103], int source, int sink) { int v[103], c[103], p[103], ret = 0, i, j; for (;;) { for (i = 0; i < n; i++) v[i] = c[i] = 0; for (c[source] = 0x7FFFFFF;;) { for (j = -1, i = 0; i < n; i++) if (!v[i] && c[i] && (j == -1 || c[i] > c[j])) j = i; if (j < 0) return ret; if (j == sink) break; for (v[j] = 1, i = 0; i < n; i++) if (mat[j][i] > c[i] && c[j] > c[i]) c[i] = mat[j][i] < c[j] ? mat[j][i] : c[j], p[i] = j; } for (ret += j = c[i = sink]; i != source; i = p[i]) mat[p[i]][i] -= j, mat[i][p[i]] += j; } } void solve() { for (int i = 1; i < n; i++) { reset(); int ans1 = max_flow(n, mat, 0, i); reset(); int ans2 = max_flow(n, mat, i, 0); if (ans1 + ans2 < ans) ans = ans1 + ans2; } printf("%d\n", ans + ansf); } int main() { while (scanf("%d%d", &n, &m), n) { memset(maty, -1, sizeof(maty)); ans = 0x7FFFFFF; ansf = 0; for (int i = 0; i < m; i++) { int x, y, w; scanf("%d%d%d", &x, &y, &w); if (w <= 0) ansf += w; else maty[x][y] = w; } solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; using ll = long long; using ull = unsigned long long; constexpr ll TEN(int n) { return (n == 0) ? 1 : 10 * TEN(n - 1); } int bsr(int x) { return 31 - __builtin_clz(x); } template <class E> using Graph = vector<vector<E>>; template <class D, D INF> struct BidirectedCut { D sum = INF; template <class E> BidirectedCut(Graph<E> g) { int n = (int)g.size(); int m_a = -1, m_b = -1; vector<D> dist_base(n, 0); for (int m = n; m > 1; m--) { int a, b; auto dist = dist_base; for (int i = 0; i < m; i++) { a = b; b = max_element(begin(dist), end(dist)) - begin(dist); if (i == m - 1) sum = min(sum, dist[b]); dist[b] = -INF; for (E e : g[b]) { if (e.to == m_b) e.to = m_a; if (dist[e.to] == -INF) continue; dist[e.to] += e.dist; } } m_a = a; m_b = b; g[a].insert(end(g[a]), begin(g[b]), end(g[b])); g[b].clear(); dist_base[b] = -INF; } } }; bool solve() { int n, m; cin >> n >> m; if (!n) return false; struct Edge { int to; ll dist; }; Graph<Edge> g(n); auto addEdge = [&](int a, int b, ll c) { g[a].push_back(Edge{b, c}); g[b].push_back(Edge{a, c}); }; for (int i = 0; i < m; i++) { int a, b; ll c; cin >> a >> b >> c; addEdge(a, b, c); } cout << BidirectedCut<ll, TEN(15)>(g).sum << endl; return true; } int main() { while (solve()) { } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0; vector<pair<long long, long long> > edge[n]; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c < 0) sum += c; edge[x].push_back(make_pair(y, c)); } if (sum < 0) { cout << sum << endl; continue; } long long res = 1234567890123456789; { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (!can[i][edge[now][j].first]) { pos.push(edge[now][j].first); } } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { cout << 0 << endl; goto next; } } for (int iii = 0; iii < int(n); ++iii) for (int jjj = 0; jjj < int(edge[iii].size()); ++jjj) for (int kkk = 0; kkk < int(n); ++kkk) for (int lll = 0; lll < int(edge[kkk].size()); ++lll) { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (now == iii && j == jjj) continue; if (now == kkk && j == lll) continue; if (!can[i][edge[now][j].first]) { pos.push(edge[now][j].first); } } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { if (iii == kkk && jjj == lll) { res = min(res, edge[iii][jjj].second); } else { res = min(res, edge[iii][jjj].second + edge[iii][jjj].second); } } } cout << res << endl; next:; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
/////节点号 【1,n】; #include<cstdio> #include<cstring> #include<algorithm> using namespace std; //使用时: nbs 置为 -1;num 置为0; //s为源点,t为汇点 #define INF 2100000000 #define M 200////dian #define N 100000///bian int nbs[M+10];///点的边链表 int nbsnext[M+10];//// int next[N],ev[N];///结构里的 int c[N];///流量 int d[M+10];///距离 int source,sink,n,m; int num=0;////边的条数 int stack[N];int Q[M+10]; int maxflow() { int max=0,i;int top=0; ev[num]=source;///num 边的数目 stack[0]=num; while(top>=0) { int v=ev[stack[top]]; if(v==sink) { int f,u; for(f=INF,i=1;i<=top;i++) if(f>c[stack[i]]) f=c[stack[i]],u=i; for(i=1;i<=top;i++) { c[stack[i]]-=f; c[stack[i]^1]+=f; } max+=f;////每一次增加的最大流 top=u-1; } else { for(i=nbsnext[v];i!=-1;i=next[i]) { if(d[ev[i]]==-1||c[i]==0) continue; if(d[v]+1==d[ev[i]]) break; } if((nbsnext[v]=i)!=-1) stack[++top]=i; else d[v]=-1,top--; } } return max; } void addedge(int u,int v,int cc) { next[num]=nbs[u]; nbs[u]=num; ev[num]=v; c[num]=cc; num++; next[num]=nbs[v]; nbs[v]=num; ev[num]=u; c[num]=0; num++; } int relabel() { int i,j; memset(d,-1,sizeof(d)); int cur=0,tail=1; Q[1]=source; d[source]=0;/////s为源点 nbsnext[source]=nbs[source]; int tmp,v; while(cur<tail) { tmp=Q[++cur]; for(i=nbs[tmp];i!=-1;i=next[i]) { if(c[i]&&d[v=ev[i]]==-1) { nbsnext[v]=nbs[v]; d[v]=d[tmp]+1; Q[++tail]=v; } } } if(d[sink]==-1) return 0; return 1; } void init() { memset(nbs,0xff,sizeof(nbs)); num=0; } int dinic() { int ans=0; while(relabel()) ans+=maxflow(); return ans; } //int maxn=150; int g[150][150]; inline int min(int a, int b) { return a<b?a:b; } int main() { int a,b,c; while(scanf("%d%d",&n,&m)!=EOF) { if (n==0 && m==0) break; //N=n; //Init(); memset(g, 0, sizeof(g)); int ans=0; for (int i=0; i<m; i++) { scanf("%d %d %d", &a, &b, &c); a++; b++; g[a][b]+=c; ans+=c; } //inf=ans; for (int i=2; i<=n; i++) { source=1; sink=i; int tmp=0; init(); for (int ii=1; ii<=n; ii++) for (int jj=1; jj<=n; jj++) { if (ii==jj) continue; if (g[ii][jj]<0) tmp+=g[ii][jj]; if (g[ii][jj]>0) addedge(ii, jj, g[ii][jj]); } ans=min(ans, dinic()+tmp); } printf("%d\n", ans); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline bool minimize(T1& a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1& a, T2 b) { return a < b && (a = b, 1); } int const inf = 1 << 29; struct unicycle_graph { int N; using graph_type = pair<int, long long>; vector<vector<graph_type>> g; vector<int> cycle_vs_; vector<bool> in_cycle_v_; using edge = pair<int, int>; set<edge> in_cycle_edge_; unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {} void add_edge(int f, int t, int c) { g[f].emplace_back(t, c); g[t].emplace_back(f, c); } void build() { vector<bool> vis(N); vector<int> prev(N); std::function<bool(int, int)> dfs = [&](int x, int p) { for (auto const& e : g[x]) { int to, cost; tie(to, cost) = e; if (to == p) { continue; } prev[to] = x; if (vis[to]) { for (int curr = x; curr != to; curr = prev[curr]) { cycle_vs_.push_back(curr); in_cycle_v_[curr] = 1; in_cycle_edge_.emplace(prev[curr], curr); in_cycle_edge_.emplace(curr, prev[curr]); } cycle_vs_.push_back(to); in_cycle_v_[to] = 1; in_cycle_edge_.emplace(to, prev[to]); in_cycle_edge_.emplace(prev[to], to); reverse((cycle_vs_).begin(), (cycle_vs_).end()); return true; } else { vis[to] = 1; if (dfs(to, x)) { return true; } } } return false; }; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; if (dfs(i, -1)) { break; } } } } bool in_cycle(int x) { return in_cycle_v_[x]; } bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); } vector<int> const& cycle_vertices() { return cycle_vs_; } }; struct union_find { vector<int> rank_, size_, rid_; union_find(int n) { rank_.resize(n); rid_.assign(n, -1); size_.resize(n, 1); } void operator()(int u, int v) { u = operator[](u), v = operator[](v); if (u == v) { return; } size_[u] = size_[v] = size_[u] + size_[v]; if (rank_[u] < rank_[v]) { rid_[u] = v; } else { rid_[v] = u; if (rank_[u] == rank_[v]) { rank_[u]++; } } } int operator[](int x) { if (rid_[x] < 0) return x; else return rid_[x] = operator[](rid_[x]); } int size_of(int x) { return size_[x]; } }; int main() { for (int N, M; cin >> N >> M && (N | M);) { unicycle_graph ug(N); union_find uf(N); vector<pair<long long, unicycle_graph::edge>> all_edges; for (int i = 0; i < (int)M; i++) { int x, y, c; cin >> x >> y >> c; all_edges.emplace_back(c, make_pair(x, y)); ug.add_edge(x, y, c); uf(x, y); } if (uf.size_of(0) == N) { cout << 0 << endl; return 0; } ug.build(); sort((all_edges).begin(), (all_edges).end()); vector<long long> ccosts; long long mcost = 1e10; for (int i = 0; i < (int)M; i++) { auto c = all_edges[i].first; auto e = all_edges[i].second; if (ug.in_cycle_edge(e.first, e.second)) { ccosts.push_back(c); } else { minimize(mcost, c); } } if (ccosts.size() > 1) { cout << min(ccosts[0] + ccosts[1], mcost) << endl; } else { cout << mcost << endl; } } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <iostream> #include <sstream> #include <cstdlib> #include <cmath> #include <map> #include <set> #include <queue> #include <string> #include <vector> #include <algorithm> #include <functional> #include <cstdio> #include <numeric> #include <bitset> #include <stack> using namespace std; typedef struct { int to, cap, rev; } Edge; const int MAX_N = 100, INF = 1 << 30; bool used[MAX_N]; void add_edge(vector<vector<Edge> >& G, int from, int to, int cap) { Edge e1 = { to, cap, G[to].size() }; G[from].push_back(e1); Edge e2 = { from, 0, G[from].size()-1 }; G[to].push_back(e2); } int solve(vector<vector<Edge> >& G, int s, int t, int f) { if (s == t) return f; used[s] = true; for (unsigned int i = 0; i < G[s].size(); ++i) { Edge& e = G[s][i]; if (!used[e.to] && e.cap > 0) { int d = solve(G, e.to, t, min(e.cap, f)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(vector<vector<Edge> > G, int s, int t) { int flow = 0; for ( ; ; ) { memset(used, 0, sizeof(used)); int f = solve(G, s, t, INF); if (f == 0) return flow; flow += f; } } int main() { int n, m; while (~scanf("%d %d", &n, &m)) { if (n == 0 && m == 0) break; vector<vector<Edge> > G(n); int rev = 0; for (int i = 0; i < m; ++i) { int x, y, c; scanf("%d %d %d", &x, &y, &c); if (c < 0) { rev += c; c = 0; } add_edge(G, x, y, c); } int ans = INF; for (int i = 0; i < n; ++i) { for (int j = i+1; j < n; ++j) { ans = min(ans, max_flow(G, i, j)); } } printf("%d\n", ans+rev); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline bool minimize(T1& a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1& a, T2 b) { return a < b && (a = b, 1); } int const inf = 1 << 29; struct unicycle_graph { int N; using graph_edge_t = pair<int, long long>; vector<vector<graph_edge_t>> g; vector<int> cycle_vs_; vector<bool> in_cycle_v_; using edge = pair<int, int>; set<edge> in_cycle_edge_; unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {} void add_edge(int f, int t, int c) { g[f].emplace_back(t, c); g[t].emplace_back(f, c); } bool remove_edge(int f, int t) { for (int i = 0; i < (int)g[f].size(); i++) { if (g[f][i].first == t) { g[f].erase(g[f].begin() + i); for (int k = 0; k < (int)g[t].size(); k++) { if (g[t][k].first == f) { g[t].erase(g[t].begin() + k); if (in_cycle_edge(f, t)) { cycle_vs_.clear(); in_cycle_v_.clear(); in_cycle_v_.resize(N); in_cycle_edge_.clear(); } return true; } } } } return false; } void build() { vector<bool> vis(N); vector<int> prev(N); std::function<bool(int, int)> dfs = [&](int x, int p) { for (auto const& e : g[x]) { int to, cost; tie(to, cost) = e; if (to == p) { continue; } prev[to] = x; if (vis[to]) { for (int curr = x; curr != to; curr = prev[curr]) { cycle_vs_.push_back(curr); in_cycle_v_[curr] = 1; in_cycle_edge_.emplace(prev[curr], curr); in_cycle_edge_.emplace(curr, prev[curr]); } cycle_vs_.push_back(to); in_cycle_v_[to] = 1; in_cycle_edge_.emplace(to, prev[to]); in_cycle_edge_.emplace(prev[to], to); reverse((cycle_vs_).begin(), (cycle_vs_).end()); return true; } else { vis[to] = 1; if (dfs(to, x)) { return true; } } } return false; }; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; if (dfs(i, -1)) { break; } } } } bool in_cycle(int x) { return in_cycle_v_[x]; } bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); } vector<int> const& cycle_vertices() { return cycle_vs_; } }; int count_components(unicycle_graph::vector<vector<graph_edge_t>>& g) { int N = g.size(); vector<bool> vis(N); std::function<void(int)> dfs = [&](int x) { for (auto&& e : g[x]) { if (vis[e.first]) { continue; } vis[e.first] = 1; dfs(e.first); } }; int ret = 0; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; dfs(i); ret++; } } return ret; } int main() { for (int N, M; cin >> N >> M && (N | M);) { unicycle_graph ug(N); vector<pair<long long, unicycle_graph::edge>> all_edges; for (int i = 0; i < (int)M; i++) { int x, y; long long c; cin >> x >> y >> c; all_edges.emplace_back(c, make_pair(x, y)); ug.add_edge(x, y, c); } ug.build(); vector<long long> ccosts; long long mcost = 1e10; for (int i = 0; i < (int)M; i++) { auto c = all_edges[i].first; auto e = all_edges[i].second; if (ug.in_cycle_edge(e.first, e.second)) { ccosts.push_back(c); } else { minimize(mcost, c); } } sort((ccosts).begin(), (ccosts).end()); if (ccosts.size() > 1) mcost = min({mcost, ccosts[0] + ccosts[1], mcost + ccosts[0]}); if (mcost == 1e10) cout << 0 << endl; else cout << mcost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct FordFulkerson { struct Edge { int to, cap, rev; Edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {} }; int V; vector<vector<Edge> > G; vector<bool> used; FordFulkerson(int V) : V(V) { G.resize(V); used.assign(V, false); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (1) { used.assign(V, false); int f = dfs(s, t, INF); if (f == 0) break; flow += f; } return flow; } }; int n, m; void solve() { int s = n; int t = n + 1; int sum = 0; vector<pair<pair<int, int>, int> > vec; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) vec.push_back(pair<pair<int, int>, int>(make_pair(x, y), c)); else sum += c; } if (m == 1) { cout << vec[0].second << endl; return; } int ans = INF; for (int i = 0; i < (vec.size()); i++) { FordFulkerson flow(t + 1); flow.add_edge(s, vec[i].first.first, INF); for (int j = 0; j < (m); j++) { flow.add_edge(vec[j].first.first, vec[j].first.second, vec[j].second); flow.add_edge(vec[j].first.second, vec[j].first.first, vec[j].second); if (i != j) flow.add_edge(vec[j].first.second, t, INF); } ans = min(ans, sum + flow.max_flow(s, t)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> using namespace std; int n,m; int x[100],y[100],c[100]; bool use[100]; int par[100],rank[100]; int find(int x){ if(par[x] == x)return x; else return par[x] = find(par[x]); } bool union2(void){ int i; int a,b,c; for(i=0;i<n;i++){ par[i] = i; rank[i] = 0; } c = n; for(i=0;i<m;i++){ if(!use[i]){ a = find(x[i]); b = find(y[i]); if(a != b){ c--; if(rank[a] < rank[b]){ par[a] = b; }else{ par[b] = a; if(rank[a] == rank[b])rank[a]++; } } } } if(c == 2)return true; return false; } int main(){ int cost; int i,j; while(1){ cin >> n >> m; if(!n && !m)break; for(i=0;i<m;i++){ cin >> x[i] >> y[i] >> c[i]; use[i] = false; } if(union2())cost = 0; else cost = 2147483647; for(i=0;i<m;i++){ for(j=i;j<m;j++){ use[i] = true; use[j] = true; if(union2()){ if(i==j){ if(cost > c[i])cost = c[i]; }else{ if(cost > c[i] + c[j])cost = c[i] + c[j]; } } use[i] = false; use[j] = false; } } cout << cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; //The House of Huge Family public class Main{ class E{ int s, t, id; long c; public E(int s, int t, long c, int id) { this.s = s; this.t = t; this.c = c; this.id = id; } } int n, m; List<E>[] adj; boolean isCon(int f1, int f2){ boolean[] u = new boolean[n]; List<Integer> l = new ArrayList<Integer>(); l.add(0); u[0] = true; int N = 1; while(!l.isEmpty()){ List<Integer> next = new ArrayList<Integer>(); for(int i:l){ N++; for(E e:adj[i])if(!u[e.t]&&e.id!=f1&&e.id!=f2){ u[e.t] = true; next.add(e.t); } } l = next; } return N==n; } @SuppressWarnings("unchecked") void run(){ Scanner sc = new Scanner(System.in); for(;;){ n = sc.nextInt(); m = sc.nextInt(); if((n|m)==0)break; adj = new List[n]; for(int i=0;i<n;i++)adj[i]=new ArrayList<E>(); int N = 0; long M = 0; List<Long> cost = new ArrayList<Long>(); while(m--!=0){ int s = sc.nextInt(), t = sc.nextInt(); long c = sc.nextInt(); if(c<=0)M+=c; else{ cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++; } } long min = 1L << 40; if(isCon(-1, -1))min = 0; for(int i=0;i<N;i++)if(isCon(i, -1))min = Math.min(min, cost.get(i)); for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)if(isCon(i, j))min = Math.min(min, cost.get(i)+cost.get(j)); System.out.println(M+min); } } public static void main(String[] args) { new Main().run(); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 29; template <class T> struct AdjMatrix : public vector<vector<T> > { AdjMatrix(int n, const vector<T> &v = vector<T>()) : vector<vector<T> >(n, v) {} }; template <class T> T augment(const AdjMatrix<T> &capa, int src, int snk, AdjMatrix<T> &flow) { int n = capa.size(); vector<int> parent(n, -1); parent[src] = -2; T water = 0; queue<pair<T, int> > qu; qu.push(make_pair(INF, src)); while (!qu.empty()) { pair<T, int> a = qu.front(); qu.pop(); int u = a.second; T w = a.first; if (u == snk) { water = w; break; } for (int v = 0; v < (n); v++) if (parent[v] == -1 && capa[u][v] - flow[u][v] > 0) { qu.push(make_pair(min(w, capa[u][v] - flow[u][v]), v)); parent[v] = u; } } if (water == 0) return 0; for (int v = snk, u = parent[snk]; v != src; v = u, u = parent[u]) { flow[u][v] += water; flow[v][u] -= water; } return water; } template <class T> T FordFulkerson(const AdjMatrix<T> &capa, int src, int snk) { int n = capa.size(); AdjMatrix<T> flow(n, vector<T>(n)); T ans = 0; while (1) { T water = augment(capa, src, snk, flow); if (water == 0) break; ans += water; } return ans; } int main() { for (int n, m; scanf("%d%d", &n, &m), n;) { int ans = 0; AdjMatrix<int> adj(n, vector<int>(n)); for (int i = 0; i < (m); i++) { int u, v, w; scanf("%d%d%d", &u, &v, &w); if (w < 0) ans += w; adj[u][v] = adj[v][u] = max(w, 0); } int ans2 = INF; for (int v = 0; v < (n); v++) for (int u = 0; u < (v); u++) ans2 = min(ans2, FordFulkerson(adj, u, v)); printf("%d\n", ans + ans2); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { while (true) { long long n, m; cin >> n >> m; if (n == 0 && m == 0) break; assert(n >= m); long long sum = 0, mn = 1234567890123456789LL, mn1 = 1234567890321456789LL; vector<pair<long long, long long> > edge[n]; for (int i = 0; i < int(m); ++i) { long long x, y, c; cin >> x >> y >> c; if (c <= 0) sum += c; else { if (c <= mn) { mn1 = mn; mn = c; } edge[x].push_back(make_pair(y, c)); edge[y].push_back(make_pair(x, c)); } } long long res = mn + mn1; { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); if (can[i][now]) continue; can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { pos.push(edge[now][j].first); } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { cout << sum << endl; goto next; } } for (int iii = 0; iii < int(n); ++iii) for (int jjj = 0; jjj < int(edge[iii].size()); ++jjj) { bool can[111][111] = {}; for (int i = 0; i < int(n); ++i) { queue<long long> pos; pos.push(i); while (!pos.empty()) { long long now = pos.front(); pos.pop(); if (can[i][now]) continue; can[i][now] = true; for (int j = 0; j < int(edge[now].size()); ++j) { if (now == iii && j == jjj) continue; pos.push(edge[now][j].first); } } } for (int i = 0; i < int(n); ++i) for (int j = 0; j < int(n); ++j) if (!can[i][j] && !can[j][i]) { res = min(res, edge[iii][jjj].second); } } cout << sum + res << endl; next:; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int par[100], rnk[100]; void init(int N) { for (int i = 0; i < N; i++) { par[i] = i; rnk[i] = 0; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rnk[x] < rnk[y]) { par[x] = y; } else { par[y] = x; if (rnk[y] == rnk[x]) { rnk[x]++; } } } bool same(int x, int y) { return (find(x) == find(y)); } struct Edge { int u, v, cost; }; bool comp(const Edge &e1, const Edge &e2) { return e1.cost < e2.cost; } Edge es[10000]; int V, E, min_cost; int kruskal() { sort(es, es + E, comp); init(V); int res = 0; for (int i = 0; i < E; i++) { Edge &e = es[i]; if (!same(e.u, e.v)) { unite(e.u, e.v); res += e.cost; min_cost = min(min_cost, e.cost); } } return res; } int main() { while (cin >> V >> E, V) { for (int i = 0; i < E; i++) { cin >> es[i].u >> es[i].v >> es[i].cost; } min_cost = INT_MAX; kruskal(); cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; int N, M; vector<Edge> G[110], cycle; int color[110]; void dfs(int v, vector<Edge> u) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { Edge &e = G[v][i]; int to = e.to; if (color[to] == -1) { u.push_back(G[v][i]); dfs(to, u); u.pop_back(); } else if (color[to] == 0) { color[to] = 1; u.push_back(G[v][i]); cycle = u; u.pop_back(); throw 0; } } color[v] = 1; } vector<vector<Edge>> cycles; bool solve() { vector<Edge> vec; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { try { if (color[i] == -1) { dfs(i, vec); } } catch (...) { cycles.push_back(cycle); cycle.clear(); } vec.clear(); } return (cycles.size() > 0); } void init() { cycle.clear(); cycles.clear(); for (int i = 0; i < 110; i++) { G[i].clear(); } } int main() { int s, t, cost; while (cin >> N >> M, N) { int min_cost = INT_MAX; init(); for (int i = 0; i < M; i++) { cin >> s >> t >> cost; G[s].push_back(Edge(s, t, cost)); min_cost = min(min_cost, cost); } if (solve()) { set<pair<int, int>> st; vector<int> v; if (cycles.size() == 1) { for (int i = 0; i < (int)cycles[0].size(); i++) { v.push_back(cycles[0][i].cost); st.insert(pair<int, int>(cycles[0][i].from, cycles[0][i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); i++) { Edge &e = G[i][j]; if (st.count(pair<int, int>(e.from, e.to)) == 0) { min_cost = min(min_cost, e.cost + min(0, v[0])); } } } } else { int min_cost = 0; for (int i = 0; i < (int)cycles.size(); i++) { vector<int> v; for (int j = 0; j < (int)cycles[i].size(); j++) { v.push_back(cycles[i][j].cost); } sort(v.begin(), v.end()); if (v[0] < 0) { min_cost += v[0]; } } } } cout << min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct FordFulkerson { struct Edge { int to, cap, rev; Edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {} }; int V; vector<vector<Edge> > G; vector<bool> used; FordFulkerson(int V) : V(V) { G.resize(V); used.assign(V, false); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (1) { used.assign(V, false); int f = dfs(s, t, INF); if (f == 0) break; flow += f; } return flow; } }; int n, m; void solve() { int s = n; int t = n + 1; int sum = 0; vector<pair<pair<int, int>, int> > vec; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) vec.push_back(pair<pair<int, int>, int>(make_pair(x, y), c)); else sum += c; } if (m == 1) { cout << vec[0].second << endl; return; } int ans = INF; for (int i = 0; i < (vec.size()); i++) { FordFulkerson flow(t + 1); flow.add_edge(s, vec[i].first.first, INF); for (int j = 0; j < (vec.size()); j++) { flow.add_edge(vec[j].first.first, vec[j].first.second, vec[j].second); flow.add_edge(vec[j].first.second, vec[j].first.first, vec[j].second); if (i != j) flow.add_edge(vec[j].first.second, t, INF); } ans = min(ans, sum + flow.max_flow(s, t)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct FordFulkerson { struct Edge { int to, cap, rev; Edge(int to = 0, int cap = 0, int rev = 0) : to(to), cap(cap), rev(rev) {} }; int V; vector<vector<Edge> > G; vector<bool> used; FordFulkerson(int V) : V(V) { G.resize(V); used.assign(V, false); } void add_edge(int from, int to, int cap) { G[from].push_back(Edge(to, cap, G[to].size())); G[to].push_back(Edge(from, 0, G[from].size() - 1)); } int dfs(int v, int t, int f) { if (v == t) return f; used[v] = true; for (int i = 0; i < G[v].size(); i++) { Edge &e = G[v][i]; if (!used[e.to] && e.cap > 0) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int max_flow(int s, int t) { int flow = 0; while (1) { used.assign(V, false); int f = dfs(s, t, INF); if (f == 0) break; flow += f; } return flow; } }; int n, m; void solve() { int s = n; int t = n + 1; int sum = 0; vector<pair<pair<int, int>, int> > vec; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; if (c > 0) vec.push_back(pair<pair<int, int>, int>(make_pair(x, y), c)); else sum += c; } if (vec.size() == 1) { cout << vec[0].second << endl; return; } else if (vec.size() == 0) { cout << sum << endl; return; } int ans = INF; for (int i = 0; i < (vec.size()); i++) { FordFulkerson flow(t + 1); flow.add_edge(s, vec[i].first.first, INF); for (int j = 0; j < (vec.size()); j++) { flow.add_edge(vec[j].first.first, vec[j].first.second, vec[j].second); flow.add_edge(vec[j].first.second, vec[j].first.first, vec[j].second); if (i != j) flow.add_edge(vec[j].first.second, t, INF); } ans = min(ans, sum + flow.max_flow(s, t)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> using namespace std; int n,m; int x[100],y[100],c[100]; bool use[100]; int par[100],rank[100]; int find(int x){ if(par[x] == x)return x; else return par[x] = find(par[x]); } bool union2(void){ int i; int a,b,c; for(i=0;i<n;i++){ par[i] = i; rank[i] = 0; } c = n; for(i=0;i<m;i++){ if(!use[i]){ a = find(x[i]); b = find(y[i]); if(a != b){ c--; if(rank[a] < rank[b]){ par[a] = b; }else{ par[b] = a; if(rank[a] == rank[b])rank[a]++; } } } } if(c == 2)return true; return false; } int main(){ int cost; int i,j; while(1){ cin >> n >> m; if(!n && !m)break; for(i=0;i<m;i++){ cin >> x[i] >> y[i] >> c[i]; use[m] = false; } if(union2())cost = 0; else cost = 2147483647; for(i=0;i<m;i++){ for(j=i;j<m;j++){ use[i] = true; use[j] = true; if(union2()){ if(i==j){ if(cost > c[i])cost = c[i]; }else{ if(cost > c[i] + c[j])cost = c[i] + c[j]; } } use[i] = false; use[j] = false; } } cout << cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; struct Edge { int from, to, cost; Edge(int from, int to, int cost) : from(from), to(to), cost(cost) {} }; int N, M; vector<Edge> G[110], cycle; int color[110]; void dfs(int v, vector<Edge> u) { color[v] = 0; for (int i = 0; i < (int)G[v].size(); i++) { Edge &e = G[v][i]; int to = e.to; if (color[to] == -1) { u.push_back(G[v][i]); dfs(to, u); u.pop_back(); } else if (color[to] == 0) { color[to] = 1; u.push_back(G[v][i]); cycle = u; u.pop_back(); throw 0; } } color[v] = 1; } bool solve() { vector<Edge> vec; memset(color, -1, sizeof(color)); for (int i = 0; i < N; i++) { try { if (color[i] == -1) { dfs(i, vec); } } catch (...) { break; } vec.clear(); } return (cycle.size() > 0); } void init() { cycle.clear(); for (int i = 0; i < 110; i++) { G[i].clear(); } } class Union_Find { public: int par[110], rank[110], size[110], gnum; Union_Find(int N) { gnum = N; for (int i = 0; i < N; i++) { par[i] = i; rank[i] = 0; size[i] = 1; } } int find(int x) { if (par[x] == x) { return x; } return par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; size[y] += size[x]; } else { par[y] = x; size[x] += size[y]; if (rank[x] == rank[y]) { rank[x]++; } } gnum--; } bool same(int x, int y) { return (find(x) == find(y)); } int getSize(int x) { return size[find(x)]; } int groups() { return gnum; } }; int main() { int s, t, cost; while (cin >> N >> M, N) { int res = 0, min_cost = INT_MAX; init(); Union_Find uf(N); for (int i = 0; i < M; i++) { cin >> s >> t >> cost; if (cost > 0) { G[s].push_back(Edge(s, t, cost)); uf.unite(s, t); min_cost = min(min_cost, cost); } else { res += cost; } } if (uf.gnum == 1) { if (solve()) { min_cost = INT_MAX; set<pair<int, int> > st; vector<int> v; for (int i = 0; i < (int)cycle.size(); i++) { v.push_back(cycle[i].cost); st.insert(pair<int, int>(cycle[i].from, cycle[i].to)); } sort(v.begin(), v.end()); min_cost = v[0] + v[1]; for (int i = 0; i < N; i++) { for (int j = 0; j < (int)G[i].size(); i++) { Edge &e = G[i][j]; if (st.count(pair<int, int>(e.from, e.to)) == 0) { min_cost = min(min_cost, e.cost); } } } } } else { min_cost = 0; } cout << res + min_cost << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const long long maxw = 2000000000; long long g[111][111], w[111]; long long a[111], v[111], na[111]; long long Stoer_Wagner(int n) { long long i, j, pv, zj; long long best = maxw * n * n; for (i = 0; i < n; i++) v[i] = i; while (n > 1) { for (a[v[0]] = 1, i = 1; i < n; i++) { a[v[i]] = 0; na[i - 1] = i; w[i] = g[v[0]][v[i]]; } for (pv = v[0], i = 1; i < n; i++) { for (zj = -1, j = 1; j < n; j++) if (!a[v[j]] && (zj < 0 || w[j] > w[zj])) zj = j; a[v[zj]] = 1; if (i == n - 1) { if (best > w[zj]) best = w[zj]; for (i = 0; i < n; i++) g[v[i]][pv] = g[pv][v[i]] += g[v[zj]][v[i]]; v[zj] = v[--n]; break; } pv = v[zj]; for (j = 1; j < n; j++) if (!a[v[j]]) w[j] += g[v[zj]][v[j]]; } } return best; } bool vis[111]; void dfs(int u, int n) { for (int v = 0; v < n; ++v) if (g[u][v] != 0 && vis[v] == 0) { vis[v] = 1; dfs(v, n); } } bool ok(int n) { for (int i = 0; i < n; ++i) if (vis[i] == 0) return 1; return 0; } int main() { int n, m, u, v, w; while (scanf("%d%d", &n, &m) != EOF, n + m) { memset(g, 0, sizeof(g)); while (m--) { scanf("%d%d%d", &u, &v, &w); g[u][v] += w; g[v][u] += w; } printf("%lld\n", Stoer_Wagner(n)); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
UNKNOWN
### subroutines def connected?(n, nbrs, edges) used = n.times.map{false} used[0] = true q = [0] while ! q.empty? u = q.shift for v in nbrs[u] if ! edges[u][v].nil? && ! used[v] used[v] = true q << v end end end for usd in used return false if ! usd end true end ### main loop do n, m = gets.split.map(&:to_i) break if n == 0 && m == 0 min_cost = 0 nlinks = 0 links = [] edges = n.times.map{[]} nbrs = n.times.map{[]} m.times do i, j, w = gets.split.map(&:to_i) if w < 0 min_cost += w else nlinks += 1 links << [w, i, j] edges[i][j] = edges[j][i] = w nbrs[i] << j nbrs[j] << i end end links.sort! #p links #p edges #p nbrs if nlinks < n - 1 || ! connected?(n, nbrs, edges) puts min_cost next end ok = false for k in (0...nlinks) w, i, j = links[k] edges[i][j] = edges[j][i] = nil if ! connected?(n, nbrs, edges) ok = true min_cost += w break end edges[i][j] = edges[j][i] = w end if ok puts min_cost next end links2 = [] for i in (0...nlinks) li = links[i] wi = li[0] for j in ((i + 1)...nlinks) lj = links[j] wj = lj[0] links2 << [wi + wj, li, lj] end end nlinks2 = links2.length links2.sort! #p links2 for k in (0...nlinks2) ww, l1, l2 = links[k] w1, i1, j1 = l1 w2, i2, j2 = l2 edges[i1][j1] = edges[j1][i1] = nil edges[i2][j2] = edges[j2][i2] = nil if ! connected?(n, nbrs, edges) min_cost += ww break end edges[i1][j1] = edges[j1][i1] = w1 edges[i2][j2] = edges[j2][i2] = w2 end puts min_cost end
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> class unionfind { private: int size_; std::vector<int> parent; std::vector<int> rank; public: unionfind() : size_(0), parent(std::vector<int>()), rank(std::vector<int>()){}; unionfind(int size__) : size_(size__), parent(std::vector<int>(size_)), rank(std::vector<int>(size_, 0)) { std::iota(parent.begin(), parent.end(), 0); } int size() { return size_; } int root(int x) { if (parent[x] == x) return x; return parent[x] = root(parent[x]); } bool same(int x, int y) { return root(x) == root(y); } void unite(int x, int y) { x = root(x); y = root(y); if (x == y) return; if (rank[x] < rank[y]) parent[x] = y; else if (rank[x] > rank[y]) parent[y] = x; else parent[y] = x, rank[x]++; } }; using namespace std; int n, m, x[109], y[109]; long long c[109]; bool solve(vector<int> unused) { unionfind uf(n); vector<bool> used(n, true); for (int i : unused) used[i] = false; for (int i = 0; i < m; i++) { if (used[i]) uf.unite(x[i], y[i]); } int s = uf.root(0); for (int i = 1; i < n; i++) { if (s != uf.root(i)) return true; } return false; } int main() { while (cin >> n >> m, n) { for (int i = 0; i < m; i++) cin >> x[i] >> y[i] >> c[i]; long long ret = 1LL << 60; if (solve({})) ret = 0; for (int i = 0; i < m; i++) { if (solve({i})) ret = min(ret, c[i]); } for (int i = 0; i < m; i++) { for (int j = i + 1; j < m; j++) { if (solve({i, j})) ret = min(ret, c[i] + c[j]); } } cout << ret << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; struct Dinic { struct edge { int to, cap, rev; edge() {} edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {} }; int n; vector<vector<edge> > G; vector<map<int, int> > M; vector<int> level, iter; Dinic() {} Dinic(int sz) : n(sz), G(n), M(n), level(n), iter(n) {} void add_edge(int from, int to, int cap) { M[from][to] = G[from].size(); M[to][from] = G[to].size(); G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int flow(int s, int t, int lim) { int fl = 0; while (1) { bfs(s); if (level[t] < 0 || lim == 0) return fl; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, lim)) > 0) { fl += f; lim -= f; } } } int flow(int s, int t) { return flow(s, t, INF); } bool back_edge(int s, int t, int from, int to) { for (int i = 0; i < (int)G[from].size(); i++) { edge &e = G[from][i]; if (e.to == to) { if (e.cap == 0 && flow(from, to, 1) == 0) { flow(from, s, 1); flow(t, to, 1); return 1; } } } return 0; } }; int main() { int V, E; while (cin >> V >> E, V | E) { vector<int> uv, vv, cv; int minus = 0; for (int i = 0; i < E; i++) { int u, v, c; cin >> u >> v >> c; uv.push_back(u); vv.push_back(v); cv.push_back(c); if (c < 0) minus += c; } int ans = INF; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) continue; Dinic dinic(V); for (int k = 0; k < E; k++) { if (cv[k] > 0) dinic.add_edge(uv[k], vv[k], cv[k]); } int f = dinic.flow(i, j); if (f != 0) ans = min(ans, f); } } if (ans == INF) cout << 0 + minus << endl; else cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; const int INF = 1 << 28; struct Dinic { struct edge { int to, cap, rev; edge() {} edge(int to, int cap, int rev) : to(to), cap(cap), rev(rev) {} }; int n; vector<vector<edge> > G; vector<map<int, int> > M; vector<int> level, iter; Dinic() {} Dinic(int sz) : n(sz), G(n), M(n), level(n), iter(n) {} void add_edge(int from, int to, int cap) { M[from][to] = G[from].size(); M[to][from] = G[to].size(); G[from].push_back(edge(to, cap, G[to].size())); G[to].push_back(edge(from, 0, G[from].size() - 1)); } void bfs(int s) { fill(level.begin(), level.end(), -1); queue<int> que; level[s] = 0; que.push(s); while (!que.empty()) { int v = que.front(); que.pop(); for (int i = 0; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[e.to] < 0) { level[e.to] = level[v] + 1; que.push(e.to); } } } } int dfs(int v, int t, int f) { if (v == t) return f; for (int &i = iter[v]; i < (int)G[v].size(); i++) { edge &e = G[v][i]; if (e.cap > 0 && level[v] < level[e.to]) { int d = dfs(e.to, t, min(f, e.cap)); if (d > 0) { e.cap -= d; G[e.to][e.rev].cap += d; return d; } } } return 0; } int flow(int s, int t, int lim) { int fl = 0; while (1) { bfs(s); if (level[t] < 0 || lim == 0) return fl; fill(iter.begin(), iter.end(), 0); int f; while ((f = dfs(s, t, lim)) > 0) { fl += f; lim -= f; } } } int flow(int s, int t) { return flow(s, t, INF); } bool back_edge(int s, int t, int from, int to) { for (int i = 0; i < (int)G[from].size(); i++) { edge &e = G[from][i]; if (e.to == to) { if (e.cap == 0 && flow(from, to, 1) == 0) { flow(from, s, 1); flow(t, to, 1); return 1; } } } return 0; } }; int main() { int V, E; int minus = 0; while (cin >> V >> E, V | E) { Dinic dinic(V); for (int i = 0; i < E; i++) { int u, v, c; cin >> u >> v >> c; if (c <= 0) minus += c; else dinic.add_edge(u, v, c); } int ans = INF; for (int i = 0; i < V; i++) { for (int j = 0; j < V; j++) { if (i == j) continue; int f = dinic.flow(i, j); if (f != 0) ans = min(ans, f); dinic.flow(j, i); } } if (ans == INF) cout << 0 + minus << endl; else cout << ans + minus << endl; } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d <= 0) continue; Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, flow)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; int cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 0; house_i < num_of_houses; house_i++) { for (int house_j = house_i + 1; house_j < num_of_houses; house_j++) { FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%d\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int INF = 100000000; int dx[4] = {1, 0, -1, 0}; int dy[4] = {0, 1, 0, -1}; using namespace std; struct UnionFind { int size_; std::vector<int> par; std::vector<int> rank; UnionFind(int size_) : size_(size_) { par.resize(size_); rank.resize(size_); for (int i = 0; i < size_; i++) { par[i] = i; rank[i] = 0; } } int find(int x) { return par[x] == x ? x : par[x] = find(par[x]); } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; if (rank[x] < rank[y]) { par[x] = y; } else { par[y] = x; if (rank[x] == rank[y]) rank[x]++; } } bool same(int x, int y) { return find(x) == find(y); } }; int n, m; int kruskal(int v, const vector<pair<int, pair<int, int> > > &vec) { UnionFind uf(n); int ret = 0; for (int i = 0; i < (vec.size()); i++) { pair<int, pair<int, int> > pi = vec[i]; if (pi.second.first == v || pi.second.second == v) continue; if (!uf.same(pi.second.first, pi.second.second)) { uf.unite(pi.second.first, pi.second.second); ret += pi.first; } } return ret; } void solve() { vector<pair<int, pair<int, int> > > vec; int s = 0; for (int i = 0; i < (m); i++) { int x, y, c; cin >> x >> y >> c; vec.push_back(pair<int, pair<int, int> >(c, make_pair(x, y))); s += c; } int ans = INF; sort((vec).begin(), (vec).end(), [](pair<int, pair<int, int> > p1, pair<int, pair<int, int> > p2) { return p1.first > p2.first; }); for (int i = 0; i < (n); i++) { ans = min(ans, s - kruskal(i, vec)); } cout << ans << endl; } int main() { while (cin >> n >> m) { if (!n) break; solve(); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <class T1, class T2> inline bool minimize(T1& a, T2 b) { return b < a && (a = b, 1); } template <class T1, class T2> inline bool maximize(T1& a, T2 b) { return a < b && (a = b, 1); } int const inf = 1 << 29; struct unicycle_graph { int N; using graph_type = pair<int, long long>; vector<vector<graph_type>> g; vector<int> cycle_vs_; vector<bool> in_cycle_v_; using edge = pair<int, int>; set<edge> in_cycle_edge_; map<edge, long long> costs_; unicycle_graph(int n) : N(n), g(n), in_cycle_v_(n) {} void add_edge(int f, int t, int c) { g[f].emplace_back(t, c); g[t].emplace_back(f, c); costs_[{f, t}] = c; costs_[{t, f}] = c; } void build() { vector<bool> vis(N); vector<int> prev(N); std::function<bool(int, int)> dfs = [&](int x, int p) { for (auto const& e : g[x]) { int to, cost; tie(to, cost) = e; if (to == p) { continue; } prev[to] = x; if (vis[to]) { for (int curr = x; curr != to; curr = prev[curr]) { cycle_vs_.push_back(curr); in_cycle_v_[curr] = 1; in_cycle_edge_.emplace(prev[curr], curr); in_cycle_edge_.emplace(curr, prev[curr]); } cycle_vs_.push_back(to); in_cycle_v_[to] = 1; in_cycle_edge_.emplace(to, prev[to]); in_cycle_edge_.emplace(prev[to], to); reverse((cycle_vs_).begin(), (cycle_vs_).end()); return true; } else { vis[to] = 1; if (dfs(to, x)) { return true; } } } return false; }; for (int i = 0; i < (int)N; i++) { if (!vis[i]) { vis[i] = 1; if (dfs(i, -1)) { break; } } } } bool in_cycle(int x) { return in_cycle_v_[x]; } bool in_cycle_edge(int f, int t) { return in_cycle_edge_.count({f, t}); } vector<int> const& cycle_vertices() { return cycle_vs_; } }; int main() { for (int N, M; cin >> N >> M && (N | M);) { unicycle_graph ug(N); vector<pair<long long, unicycle_graph::edge>> tree_edges; vector<pair<long long, unicycle_graph::edge>> cycle_edges; vector<pair<long long, unicycle_graph::edge>> all_edges; for (int i = 0; i < (int)M; i++) { int x, y, c; cin >> x >> y >> c; all_edges.emplace_back(c, make_pair(x, y)); ug.add_edge(x, y, c); } ug.build(); sort((all_edges).begin(), (all_edges).end()); vector<int> ccosts; int mcost = inf; for (int i = 0; i < (int)M; i++) { auto c = all_edges[i].first; auto e = all_edges[i].second; if (ug.in_cycle_edge(e.first, e.second)) { ccosts.push_back(c); } else { minimize(mcost, c); } } if (ccosts.size() > 1) { cout << min(ccosts[0] + ccosts[1], mcost) << endl; } else { cout << mcost << endl; } } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<algorithm> #include<vector> #include<climits> #define MAX_N 100 using namespace std; typedef long long ll; typedef pair<ll,ll> P; typedef pair<ll, P > PP; ll n,m; ll par[MAX_N]; ll pa[MAX_N]; ll rank[MAX_N]; ll mini; void init(ll); ll find(ll); void unite(ll,ll); bool same(ll,ll); PP e[MAX_N]; ll I,J; int main(){ ll sum,cnt,x,y; while(cin>>n>>m&&n&&m){ cnt=sum=0; for(int i=0;i<m;i++){ cin>>e[i].second.first>>e[i].second.second>>e[i].first; sum+=e[i].first; } cnt=sum; sort(e,e+m); reverse(e,e+m); mini=INT_MAX; for(I=0;I<n-1;I++){ for(J=I+1;J<n;J++){ sum=cnt; init(n); for(int i=0;i<m;i++){ x=find(e[i].second.first); y=find(e[i].second.second); if(pa[x]+pa[y]==3)continue; //cout<<"pa "<<pa[x]<<' '<<pa[y]<<endl; //cout<<"no "<<e[i].second.first<<' '<<e[i].second.second<<endl; sum-=e[i].first; unite(e[i].second.first,e[i].second.second); } //cout<<sum<<endl; if(mini>sum)mini=sum; } } cout<<mini<<endl; } } void init(ll n){ for(int i=0;i<n;i++){ pa[i]=0; if(i==I)pa[i]=1; if(i==J)pa[i]=2; par[i]=i; rank[i]=0; } } ll find (ll x){ if(par[x]==x){ return x; }else{ return par[x]=find(par[x]); } } void unite(ll x,ll y){ x=find(x); y=find(y); if(x==y)return; if(rank[x]<rank[y]){ par[x]=y; pa[y]=max(pa[x],pa[y]); }else{ par[y]=x; pa[x]=max(pa[y],pa[x]); if(rank[x]==rank[y])rank[x]++; } } bool same(ll x,ll y){ return find(x)==find(y); }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; long long flow; Node(int to, int idx, long long flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; long long dfs(int src, long long flow, int sink) { if (src == sink) return flow; long long res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; long long d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, long long flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } long long compute_maxflow(int src, int sink) { long long res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); long long tmp = dfs(src, numeric_limits<long long>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; class UnionFindTree { private: int* par; int* rank; public: UnionFindTree(int n) { par = new int[n](); rank = new int[n](); for (int i = 0; i < n; i++) { par[i] = i; rank[i] = 0; } } ~UnionFindTree() { delete[] rank; delete[] par; } int find(int x) { if (par[x] == x) { return x; } else { return par[x] = find(par[x]); } } void unite(int x, int y) { x = find(x); y = find(y); if (x == y) return; par[x] = y; } bool same(int x, int y) { return find(x) == find(y); } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); UnionFindTree uft(num_of_houses); long long bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to; long long cost; scanf("%d %d %lld", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0LL, cost)); uft.unite(from, to); } int tree_count = 0; bool visited[101]; memset(visited, false, sizeof(visited)); for (int house_i = 0; house_i < num_of_houses; house_i++) { if (!visited[uft.find(house_i)]) tree_count++; visited[uft.find(house_i)] = true; } long long res = numeric_limits<long long>::max(); if (tree_count >= 2) { res = 0; } else { for (int house_i = 0; house_i < num_of_houses; house_i++) { int house_j = uft.find(house_i); if (house_i == house_j) continue; FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(house_i, house_j)); } } printf("%lld\n", bonus + res); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<algorithm> using namespace std; int main(){ int n,m; int x[100],y[100],c[100]; int r[100],tmp1,tmp2; int cost,total; int i,j; while(1){ cin >> n >> m; if(!n && !m)break; total = 0; for(i=0;i<m;i++){ cin >> x[i] >> y[i] >> c[i]; total += c[i]; } for(i=0;i<n;i++)r[i] = i; for(i=0;i<m;i++){ for(j=i+1;j<m;j++){ if(c[i] < c[j]){ swap(x[i],x[j]); swap(y[i],y[j]); swap(c[i],c[j]); } } } cost = 0; for(i=0;i<m;i++){ tmp1 = r[0]; tmp2 = -1; for(j=1;j<n;j++){ if(tmp1 != r[j]){ if(tmp2 < 0)tmp2 = r[j]; else if(tmp2 != r[j])break; } } if(j==n)break; if(r[x[i]] != r[y[i]]){ cost += c[i]; tmp1 = r[y[i]]; for(j=0;j<n;j++){ if(r[j] == tmp1)r[j] = r[x[i]]; } } } i++; while(c[i]>=0 && i<m){ if(r[x[i]] == r[y[i]])cost += c[i]; i++; } cout << total - cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; template <typename T> inline bool checkmin(T &a, const T &b) { return a > b ? a = b, 1 : 0; } template <typename T> inline bool checkmax(T &a, const T &b) { return a < b ? a = b, 1 : 0; } const int MAXN = 100 + 10; struct edge { int v, c, opp; bool real, ban; edge() {} edge(int _v, int _c, int _opp, bool _real) : v(_v), c(_c), opp(_opp), real(_real), ban(false) {} }; vector<edge> adj[MAXN]; bool vis[MAXN]; int n, m; void dfs(int u) { vis[u] = true; for (int i = 0; i < (((int)(adj[u].size()))); ++i) { if (adj[u][i].ban) continue; int v = adj[u][i].v; if (vis[v]) continue; dfs(v); } } bool check() { fill(vis, vis + n, false); dfs(0); for (int i = 0; i < (n); ++i) if (!vis[i]) return true; return false; } int main() { while (scanf("%d%d", &n, &m) == 2 && n + m) { int sum = 0; for (int i = 0; i < (m); ++i) { int a, b, c; scanf("%d%d%d", &a, &b, &c); if (c <= 0) { sum += c; } else { adj[a].push_back(edge(b, c, ((int)(adj[b].size())), true)); adj[b].push_back(edge(a, c, ((int)(adj[a].size())) - 1, false)); } } if (check()) { printf("%d\n", sum); continue; } int ans = INT_MAX; for (int x = 0; x < (n); ++x) { for (int i = 0; i < (((int)(adj[x].size()))); ++i) { if (!adj[x][i].real) continue; adj[x][i].ban = adj[adj[x][i].v][adj[x][i].opp].ban = true; if (check()) { checkmin(ans, sum + adj[x][i].c); } for (int y = 0; y < (n); ++y) { for (int j = 0; j < (((int)(adj[y].size()))); ++j) { if (y == x && j == i) continue; if (!adj[y][j].real) continue; adj[y][j].ban = adj[adj[y][j].v][adj[y][j].opp].ban = true; if (check()) { checkmin(ans, sum + adj[x][i].c + adj[y][j].c); } adj[y][j].ban = adj[adj[y][j].v][adj[y][j].opp].ban = false; } } adj[x][i].ban = adj[adj[x][i].v][adj[x][i].opp].ban = false; } } printf("%d\n", ans); for (int i = 0; i < (n); ++i) { adj[i].clear(); } } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; int main() { int n, m; int x[100], y[100], c[100]; int r[100], tmp1, tmp2; int cost, total; int i, j; while (1) { cin >> n >> m; if (!n && !m) break; total = 0; for (i = 0; i < m; i++) { cin >> x[i] >> y[i] >> c[i]; total += c[i]; } for (i = 0; i < n; i++) r[i] = i; for (i = 0; i < m; i++) { for (j = i + 1; j < m; j++) { if (c[i] < c[j]) { swap(x[i], x[j]); swap(y[i], y[j]); swap(c[i], c[j]); } } } cost = 0; for (i = 0; i < m; i++) { tmp1 = r[0]; tmp2 = -1; for (j = 1; j < n; j++) { if (tmp1 != r[j]) { if (tmp2 < 0) tmp2 = r[j]; else if (tmp2 != r[j]) break; } } if (j == n) break; cost += c[i]; tmp1 = r[y[i]]; for (j = 0; j < n; j++) { if (r[j] == tmp1) r[j] = r[x[i]]; } } while (c[i] >= 0 && i < m) { if (r[x[i]] == r[y[i]]) cost += c[i]; i++; } cout << total - cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include<iostream> #include<algorithm> using namespace std; int n,m; int x[100],y[100]; long long int c[100]; bool use[100]; int par[100],rank[100]; int find(int x){ if(par[x] == x)return x; else return par[x] = find(par[x]); } bool union2(void){ int i; int a,b,c; for(i=0;i<n;i++){ par[i] = i; rank[i] = 0; } c = n; for(i=0;i<m;i++){ if(!use[i]){ a = find(x[i]); b = find(y[i]); if(a != b){ c--; if(rank[a] < rank[b]){ par[a] = b; }else{ par[b] = a; if(rank[a] == rank[b])rank[a]++; } } } } /* for(i=0;i<n;i++)cout << par[i] << " "; cout << endl; */ if(c == 2)return true; return false; } int main(){ long long int cost; int i,j; while(1){ cin >> n >> m; if(!n && !m)break; for(i=0;i<m;i++){ cin >> x[i] >> y[i] >> c[i]; use[i] = false; } if(union2())cost = 0; else cost = 21474836470LL; for(i=0;i<m;i++){ use[i] = true; if(union2())cost = min(cost,c[i]); for(j=i+1;j<m;j++){ use[j] = true; if(union2())cost = min(cost,c[i]+c[j]); use[j] = false; } use[i] = false; } cout << cost << endl; } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> const int oo = 0x7fffffff; int g[110][110], d[110]; bool used[110], vst[110]; int main() { int min, n, m, max, tmp, pre, cur, a, b, w, cnt; while (scanf("%d%d", &n, &m), n) { min = oo, cnt = 0; memset(g, 0, sizeof(g)); memset(used, 0, sizeof(used)); for (int i = 0; i < m; ++i) { scanf("%d%d%d", &a, &b, &w); g[a][b] += w; g[b][a] = g[a][b]; } if (n < 2) { puts("0"); continue; } for (int i = 1; i < n; ++i) { memset(vst, 0, sizeof(vst)); vst[0] = 1; cur = pre = d[0] = 0; for (int k = 1; k < n; ++k) d[k] = g[0][k]; for (int j = i; j < n; ++j) { max = -oo; pre = cur; for (int k = 0; k < n; ++k) { if (vst[k] || used[k]) continue; if (d[k] > max) { max = d[k]; cur = k; } } vst[cur] = 1; for (int k = 0; k < n; ++k) { if (vst[k] || used[k]) continue; d[k] += g[cur][k]; } } used[cur] = 1; for (int j = 0; j < n; ++j) { if (j == pre || used[j]) continue; g[pre][j] += g[cur][j]; g[j][pre] = g[pre][j]; } tmp = 0; for (int j = 0; j < n; ++j) { if (used[j]) continue; tmp += g[cur][j]; } if (tmp < min) min = tmp; else continue; } printf("%d\n", min); } return 0; }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.Arrays; import java.util.Scanner; public class Main { static Scanner sc = new Scanner(System.in); static int N, M, E; static int[] from; static int[] cost; static boolean disjoint() { UnionFind uf = new UnionFind(N); for (int i = 0; i < N; ++i) { if (from[i] != -1) uf.union(i, from[i]); } return uf.size(0) != N; } static int solve() { if (E <= N - 2) return 0; int ret = Integer.MAX_VALUE; for (int i = 0; i < N; ++i) { if (from[i] == -1) continue; for (int j = i + 1; j < N; ++j) { if (from[j] == -1) continue; ret = Math.min(ret, cost[i] + cost[j]); } } for (int i = 0; i < N; ++i) { if (from[i] == -1) continue; int tmp = from[i]; from[i] = -1; if (disjoint()) ret = Math.min(ret, cost[i]); from[i] = tmp; } return ret; } public static void main(String[] args) throws Exception { while (true) { N = sc.nextInt(); M = sc.nextInt(); if (N == 0) break; from = new int[N]; cost = new int[N]; Arrays.fill(from, -1); int minus = 0; E = 0; for (int i = 0; i < M; ++i) { int X = sc.nextInt(); int Y = sc.nextInt(); int C = sc.nextInt(); if (C <= 0) { minus += C; } else { from[Y] = X; cost[Y] = C; ++E; } } System.out.println(solve() + minus); } } static class UnionFind { int[] set; UnionFind(int n) { set = new int[n]; Arrays.fill(set, -1); } void union(int a, int b) { int rtA = root(a); int rtb = root(b); if (rtA == rtb) { return; } set[rtA] += set[rtb]; set[rtb] = rtA; } boolean find(int a, int b) { return root(a) == root(b); } int root(int a) { if (set[a] < 0) { return a; } else { set[a] = root(set[a]); return set[a]; } } int size(int a) { return -set[root(a)]; } } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
java
import java.util.ArrayList; import java.util.List; import java.util.Scanner; //The House of Huge Family public class Main{ class E{ int s, t, c, id; public E(int s, int t, int c, int id) { this.s = s; this.t = t; this.c = c; this.id = id; } } int n, m; List<E>[] adj; boolean isCon(int f1, int f2){ boolean[] u = new boolean[n]; List<Integer> l = new ArrayList<Integer>(); l.add(0); u[0] = true; int N = 1; while(!l.isEmpty()){ List<Integer> next = new ArrayList<Integer>(); for(int i:l){ N++; for(E e:adj[i])if(!u[e.t]&&e.id!=f1&&e.id!=f2){ u[e.t] = true; next.add(e.t); } } l = next; } return N==n; } @SuppressWarnings("unchecked") void run(){ Scanner sc = new Scanner(System.in); for(;;){ n = sc.nextInt(); m = sc.nextInt(); if((n|m)==0)break; adj = new List[n]; for(int i=0;i<n;i++)adj[i]=new ArrayList<E>(); int N = 0, M = 0; List<Integer> cost = new ArrayList<Integer>(); while(m--!=0){ int s = sc.nextInt(), t = sc.nextInt(), c = sc.nextInt(); if(c<=0)M+=c; else{ cost.add(c); adj[s].add(new E(s, t, c, N)); adj[t].add(new E(t, s, c, N)); N++; } } int min = Integer.MAX_VALUE; if(isCon(-1, -1))min = 0; for(int i=0;i<N;i++)if(isCon(i, -1))min = Math.min(min, cost.get(i)); for(int i=0;i<N;i++)for(int j=i+1;j<N;j++)if(isCon(i, j))min = Math.min(min, cost.get(i)+cost.get(j)); System.out.println(M+min); } } public static void main(String[] args) { new Main().run(); } }
p00650 The House of Huge Family
Mr. Dango's family has an extremely huge number of members. Once it had about 100 members, and now it has as many as population of a city. It is jokingly guessed that the member might fill this planet in the near future. Mr. Dango's family, the huge family, is getting their new house. Scale of the house is as large as that of town. They all had warm and gracious personality and were close each other. However, in this year the two members of them became to hate each other. Since the two members had enormous influence in the family, they were split into two groups. They hope that the two groups don't meet each other in the new house. Though they are in the same building, they can avoid to meet each other by adjusting passageways. Now, you have a figure of room layout. Your task is written below. You have to decide the two groups each room should belong to. Besides you must make it impossible that they move from any rooms belonging to one group to any rooms belonging to the other group. All of the rooms need to belong to exactly one group. And any group has at least one room. To do the task, you can cancel building passageway. Because the house is under construction, to cancel causes some cost. You'll be given the number of rooms and information of passageway. You have to do the task by the lowest cost. Please answer the lowest cost. By characteristics of Mr. Dango's family, they move very slowly. So all passageways are escalators. Because of it, the passageways are one-way. Constraints * 2 ≤ n ≤ 100 * -10,000 ≤ Ci ≤ 10,000 * Y1 ... Ym can't be duplicated integer by each other. Input The input consists of multiple datasets. Each dataset is given in the following format. n m X1 Y1 C1 ... Xm Ym Cm All numbers in each datasets are integers. The integers in each line are separated by a space. The first line of each datasets contains two integers. n is the number of rooms in the house, m is the number of passageways in the house. Each room is indexed from 0 to n-1. Each of following m lines gives the details of the passageways in the house. Each line contains three integers. The first integer Xi is an index of the room, the starting point of the passageway. The second integer Yi is an index of the room, the end point of the passageway. The third integer Ci is the cost to cancel construction of the passageway. The passageways, they are escalators, are one-way. The last dataset is followed by a line containing two zeros (separated by a space). Output For each dataset, print the lowest cost in a line. You may assume that the all of integers of both the answers and the input can be represented by 32 bits signed integers. Example Input 3 2 0 1 2 1 2 1 2 1 0 1 100 2 1 0 1 0 2 1 0 1 -1 0 0 Output 1 100 0 -1
{ "input": [ "3 2\n0 1 2\n1 2 1\n2 1\n0 1 100\n2 1\n0 1 0\n2 1\n0 1 -1\n0 0" ], "output": [ "1\n100\n0\n-1" ] }
{ "input": [], "output": [] }
IN-CORRECT
cpp
#include <bits/stdc++.h> using namespace std; static const double EPS = 1e-8; const int tx[] = {0, 1, 0, -1}; const int ty[] = {-1, 0, 1, 0}; struct Node { int to; int idx; int flow; Node(int to, int idx, int flow) : to(to), idx(idx), flow(flow) {} }; class FordFulkerson { private: vector<Node>* _graph; bool* _used; int _size; int dfs(int src, int flow, int sink) { if (src == sink) return flow; int res = 0; _used[src] = true; for (int i = 0; i < _graph[src].size(); i++) { Node& e = _graph[src][i]; if (_used[e.to]) continue; if (e.flow <= 0) continue; int d = dfs(e.to, min(flow, e.flow), sink); if (d > 0) { Node& rev_e = _graph[e.to][e.idx]; e.flow -= d; rev_e.flow += d; return d; } } return 0; } public: FordFulkerson(int n) { _size = n; _graph = new vector<Node>[_size]; _used = new bool[_size]; } FordFulkerson(const FordFulkerson& f) { _size = f.size(); _graph = new vector<Node>[_size]; _used = new bool[_size]; fill((bool*)_used, (bool*)_used + _size, false); for (int i = 0; i < f.size(); i++) { for (int j = 0; j < f.graph()[i].size(); j++) { _graph[i].push_back(f.graph()[i][j]); } } } void add_edge(int from, int to, int flow) { _graph[from].push_back(Node(to, _graph[to].size(), flow)); _graph[to].push_back(Node(from, _graph[from].size() - 1, 0)); } int compute_maxflow(int src, int sink) { int res = 0; while (true) { fill((bool*)_used, (bool*)_used + _size, false); int tmp = dfs(src, numeric_limits<int>::max(), sink); if (tmp == 0) break; res += tmp; } return res; } int size() const { return _size; } vector<Node>* graph() const { return _graph; } }; int main() { int num_of_houses; int num_of_paths; while (~scanf("%d %d", &num_of_houses, &num_of_paths)) { if (num_of_houses == 0 && num_of_paths == 0) break; FordFulkerson ff(num_of_houses); int bonus = 0; for (int path_i = 0; path_i < num_of_paths; path_i++) { int from, to, cost; scanf("%d %d %d", &from, &to, &cost); if (cost < 0) bonus += cost; ff.add_edge(from, to, max(0, cost)); } int res = numeric_limits<int>::max(); for (int house_i = 1; house_i < num_of_houses; house_i++) { FordFulkerson tmp(ff); res = min(res, tmp.compute_maxflow(0, house_i)); } printf("%d\n", bonus + res); } }