【題目敘述】https://zerojudge.tw/ShowProblem?problemid=c877
【解題想法】DFS 【筆記】
#include <iostream>
#include <cstring>
#include <vector>
#include <algorithm>
using namespace std;
int houses, box, times, path;
int paths[51][51];
long long total, mx;
vector <int> v[51];
void func(int now, int timeleft, long long ans){
mx = max(mx, ans);
if (timeleft == 0 || ans + timeleft * box <= mx || mx == total) return;
else{
for (int i:v[now]){
int num = min(box, paths[now][i]);
paths[now][i] -= num;
paths[i][now] -= num;
func(i, timeleft-1, ans+num);
paths[now][i] += num;
paths[i][now] += num;
}
}
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
while (cin >> houses >> box >> times >> path){
int a, b, c;
total = 0;
memset(paths, -1, sizeof(paths));
for (int i = 0; i < 51; i++){
v[i].clear();
}
for (int i = 0; i < path; i++){
cin >> a >> b >> c;
total += c;
paths[a][b] = c;
paths[b][a] = c;
v[a].push_back(b);
v[b].push_back(a);
}
mx = 0;
func(1, times, 0);
cout << mx << "\n";
}
}