Pagini recente » Cod sursa (job #2473908) | Cod sursa (job #1324578) | Cod sursa (job #2963106) | Cod sursa (job #2480669) | Cod sursa (job #3041016)
#include <fstream>
#include <vector>
#include <queue>
using namespace std;
ifstream in ("cmcm.in");
ofstream out ("cmcm.out");
const int max_size = 7e2 + 1, INF = 1e9 + 1;
struct str{
int nod, cost;
bool operator < (const str & aux) const
{
return cost > aux.cost;
}
};
int cost[max_size][max_size], cap[max_size][max_size], pr[max_size], d[max_size], aux[max_size], n, m, t[max_size];
vector <pair <int, int>> mc[max_size];
void bf ()
{
for (int i = 1; i <= n + m + 2; i++)
{
aux[i] = INF;
}
queue <int> q;
q.push(1);
aux[1] = 0;
while (!q.empty())
{
int nod = q.front();
q.pop();
d[nod] = 0;
for (auto f : mc[nod])
{
if (cap[nod][f.first] > 0 && aux[nod] + cost[nod][f.first] < aux[f.first])
{
aux[f.first] = aux[nod] + cost[nod][f.first];
if (d[f.first] == 0)
{
d[f.first] = 1;
q.push(f.first);
}
}
}
}
}
bool djk ()
{
for (int i = 1; i <= n + m + 2; i++)
{
d[i] = INF;
pr[i] = INF;
}
d[1] = 0;
pr[1] = 0;
priority_queue <str> pq;
pq.push({1, 0});
while (!pq.empty())
{
int nod = pq.top().nod, val = pq.top().cost;
pq.pop();
if (val > d[nod])
{
continue;
}
for (auto f : mc[nod])
{
if (cap[nod][f.first] > 0 && d[nod] + cost[nod][f.first] + aux[nod] - aux[f.first] < d[f.first])
{
d[f.first] = d[nod] + cost[nod][f.first] + aux[nod] - aux[f.first];
pr[f.first] = pr[nod] + cost[nod][f.first];
t[f.first] = nod;
pq.push({f.first, d[f.first]});
}
}
}
return (d[n + m + 2] != INF);
}
int main ()
{
int k;
in >> n >> m >> k;
for (int i = 1; i <= k; i++)
{
int x, y, c;
in >> x >> y >> c;
cap[x + 1][y + n + 1] = INF;
mc[x + 1].push_back({y + n + 1, i});
mc[y + n + 1].push_back({x + 1, i});
cost[x + 1][y + n + 1] = c;
cost[y + n + 1][x + 1] = -c;
}
for (int i = 1; i <= n; i++)
{
cap[1][i + 1] = 1;
mc[1].push_back({i + 1, 0});
mc[i + 1].push_back({1, 0});
}
for (int i = 1; i <= m; i++)
{
cap[i + n + 1][n + m + 2] = 1;
mc[i + n + 1].push_back({n + m + 2, 0});
mc[n + m + 2].push_back({i + n + 1, 0});
}
bf();
int flux = 0, rez = 0;
while (djk())
{
int nod = n + m + 2, mn = INF;
while (nod != 1)
{
mn = min(mn, cap[t[nod]][nod]);
nod = t[nod];
}
if (mn <= 0)
{
continue;
}
flux += mn;
rez += mn * pr[n + m + 2];
nod = n + m + 2;
while (nod != 1)
{
cap[t[nod]][nod] -= mn;
cap[nod][t[nod]] += mn;
nod = t[nod];
}
for (int i = 1; i <= n + m + 2; i++)
{
aux[i] = d[i];
}
}
vector <int> v;
out << flux << " " << rez << '\n';
for (int i = 2; i <= n + 1; i++)
{
for (auto f : mc[i])
{
if (f.first == 1)
{
continue;
}
//out << cap[i][f.first] << " ";
if (cap[i][f.first] == INF - 1)
{
v.push_back(f.second);
}
}
//out << '\n';
}
for (auto f : v)
{
out << f << " ";
}
in.close();
out.close();
return 0;
}