solution-cf24a
题意:
在一个有 $n$ 个点,$n$ 条边单向没有重边的图中,你可以改变任意一条边的方向,但需要花钱,问你最少花多少钱改变多少条边的方向才能使得图联通。
思路:
由于是 $n$ 个点,$n$ 条边单向没有重边的图,所以为了保证联通,最后结果肯定是一个环,所以我们只需要对每个点枚举逆时针的环和顺时针的环两种情况,看看每条边是否要改变即可。
代码:
#include<bits/stdc++.h>
using namespace std;
bool vis[105]; // 是否到过这个点
long long ans = 0x3f3f3f3f;
int n;
struct edge{
int to, val;
}; vector <edge> p[105];
void dfs(int x, long long cnt, int num){
if(x == 1 and num == n){
ans = min(ans, cnt);
return ;
}
if(vis[x]) return ;
vis[x] = true;
dfs(p[x][0].to, cnt + p[x][0].val, num+1);//一种环
dfs(p[x][1].to, cnt + p[x][1].val, num+1);//另一种环
vis[x] = false;
return ;
}
int main()
{
cin>>n;
for(int i = 1; i <= n; i++)
{
int a, b, c;
cin>>a>>b>>c;
p[a].push_back(edge{b,0});
p[b].push_back({edge{a,c}});
}
dfs(1, 0, 0);
cout<<ans<<endl;
return 0;
}