Pagini recente » Cod sursa (job #672418) | Cod sursa (job #2821849) | Cod sursa (job #639524) | Cod sursa (job #1510313) | Cod sursa (job #2367929)
#include <bits/stdc++.h>
using namespace std;
class InParser {
private:
FILE *fin;
char *buff;
int sp;
char read_ch() {
++sp;
if (sp == 4096) {
sp = 0;
fread(buff, 1, 4096, fin);
}
return buff[sp];
}
public:
InParser(const char* nume) {
fin = fopen(nume, "r");
buff = new char[4096]();
sp = 4095;
}
InParser& operator >> (int &n) {
char c;
while (!isdigit(c = read_ch()) && c != '-');
int sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
InParser& operator >> (long long &n) {
char c;
n = 0;
while (!isdigit(c = read_ch()) && c != '-');
long long sgn = 1;
if (c == '-') {
n = 0;
sgn = -1;
} else {
n = c - '0';
}
while (isdigit(c = read_ch())) {
n = 10 * n + c - '0';
}
n *= sgn;
return *this;
}
};
class OutParser {
private:
FILE *fout;
char *buff;
int sp;
void write_ch(char ch) {
if (sp == 50000) {
fwrite(buff, 1, 50000, fout);
sp = 0;
buff[sp++] = ch;
} else {
buff[sp++] = ch;
}
}
public:
OutParser(const char* name) {
fout = fopen(name, "w");
buff = new char[50000]();
sp = 0;
}
~OutParser() {
fwrite(buff, 1, sp, fout);
fclose(fout);
}
OutParser& operator << (int vu32) {
if (vu32 <= 9) {
write_ch(vu32 + '0');
} else {
(*this) << (vu32 / 10);
write_ch(vu32 % 10 + '0');
}
return *this;
}
OutParser& operator << (long long vu64) {
if (vu64 <= 9) {
write_ch(vu64 + '0');
} else {
(*this) << (vu64 / 10);
write_ch(vu64 % 10 + '0');
}
return *this;
}
OutParser& operator << (char ch) {
write_ch(ch);
return *this;
}
OutParser& operator << (const char *ch) {
while (*ch) {
write_ch(*ch);
++ch;
}
return *this;
}
};
InParser fi("cmcm.in");
OutParser fo("cmcm.out");
const int NMAX = 605;
const int INF = 1e9 + 5;
int n, m, e;
int cost[NMAX][NMAX], F[NMAX][NMAX], C[NMAX][NMAX];
vector <int> G[NMAX];
int fake[2][NMAX], asd, adev[NMAX];
bool inCoada[NMAX];
int p[NMAX];
int loc[NMAX][NMAX];
void adauga(int u, int v, int c)
{
G[u].push_back(v);
G[v].push_back(u);
cost[u][v] = c;
cost[v][u] = -c;
C[u][v] = 1;
C[v][u] = 0;
}
void bellmanford()
{
for (int i = 0; i <= n + m + 1; i++)
fake[1 - asd][i] = INF;
queue <int> Q;
Q.push(0);
inCoada[0] = 1;
fake[1 - asd][0] = 0;
while (!Q.empty())
{
int nod = Q.front();
Q.pop();
inCoada[nod] = 0;
for (auto v: G[nod])
{
if (fake[1 - asd][nod] + cost[nod][v] < fake[1 - asd][v] && C[nod][v] == 1)
{
fake[1 - asd][v] = fake[1 - asd][nod] + cost[nod][v];
if (!inCoada[v])
{
Q.push(v);
inCoada[v] = 1;
}
}
}
}
}
bool dijkstra()
{
for (int i = 0; i <= n + m + 1; i++)
fake[asd][i] = INF;
priority_queue < pair<int, int> > Q;
Q.push({0, 0});
fake[asd][0] = adev[0] = 0;
while (!Q.empty())
{
pair <int, int> curr = Q.top();
Q.pop();
int nod = curr.second;
if (fake[asd][nod] != -curr.first)
continue;
for (auto v: G[nod])
{
if (fake[asd][nod] + cost[nod][v] + fake[1 - asd][nod] - fake[1 - asd][v] < fake[asd][v] && F[nod][v] != C[nod][v])
{
fake[asd][v] = fake[asd][nod] + cost[nod][v] + fake[1 - asd][nod] - fake[1 - asd][v];
adev[v] = adev[nod] + cost[nod][v];
p[v] = nod;
Q.push({-fake[asd][v], v});
}
}
}
return fake[asd][n + m + 1] != INF;
}
signed main()
{
fi >> n >> m >> e;
for (int i = 1; i <= e; i++)
{
int u, v, c;
fi >> u >> v >> c;
v += n;
loc[u][v] = i;
adauga(u, v, c);
adauga(0, u, 0);
adauga(v, n + m + 1, 0);
}
asd = 0;
bellmanford();
int rez = 0;
while (dijkstra())
{
int minim = INF;
for (int i = n + m + 1; i != 0; i = p[i])
{
minim = min(minim, C[p[i]][i] - F[p[i]][i]);
}
for (int i = n + m + 1; i != 0; i = p[i])
{
F[p[i]][i] += minim;
F[i][p[i]] -= minim;
}
rez += (adev[n + m + 1]) * minim;
asd = 1 - asd;
}
int nrm = 0;
vector <int> rrr;
for (int i = 1; i <= n; i++)
{
for (auto v: G[i])
if (F[i][v] == C[i][v] && C[i][v] == 1)
nrm++, rrr.push_back(loc[i][v]);
}
fo << nrm << " " << rez << "\n";
for (auto x: rrr)
fo << x << " ";
return 0;
}