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;
 } 
posted @ 2021-11-05 17:48  WRuperD  阅读(3)  评论(0编辑  收藏  举报  来源

本文作者:DIVMonster

本文链接:https://www.cnblogs.com/guangzan/p/12886111.html

版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。

这是一条自定义内容

这是一条自定义内容