Description

Once upon a time there was a strange kingdom, the kingdom had n cities which were connected by n directed roads and no isolated city.One day the king suddenly found that he can't get to some cities from some cities.How amazing!The king is petty so he won't build some new roads to improve this situation,but he has superpowers that he can change the direction of any road.To do this,he will gain a certain fatigue value for a certain road.The king didn't want to be too tired.So he want to know what is the smallest amount of fatigue value he will gain on the redirecting of roads so that from every city people can get to any other?

Input

The first line contains integer n (3<=n<=100) - amount of cities (and roads) in the king. Next n lines contain description of roads. Each road is described by three integers ai, bi, ci(1<=ai,bi<=n,ai!=bi,1<=ci<=100) - road is directed from city ai to city bi, redirecting it costs ci.

Output

Output single integer - the smallest amount of fatigue value the king will gain on the redirecting of roads so that from every city people can get to any other.

Sample Input

3
1 3 1
1 2 1
3 2 1
3
1 3 1
1 2 5
3 2 1

Sample Output

1
2

看上去很难,稍加分析可知n个点n条边改变方向后可以连通,只有可能是一个环,所以我们判断反向边和正向边分别的权值总和取个小的就可以了.然后如果我们对每条单向边建一条负权值的反向边,跑一遍DFS就可以了.
#include <iostream>
#include <algorithm>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N = ;
int mp[N][N];
int vis[N];
int cost;
int n,node;
void dfs(int u,int pre){
if(vis[u]==&&u!=){
return;
}
vis[u]++;
if(vis[u]==&&u==){
node = pre;
return;
}
for(int i=;i<=n;i++){
if(i==pre) continue;
if(mp[u][i]&&!vis[i]){
if(mp[u][i]<) cost+=mp[u][i];
dfs(i,u);
}
if(mp[u][i]&&vis[i]!=&&i==){
if(mp[u][i]<) cost+=mp[u][i];
dfs(i,u);
}
}
}
int main()
{ while(scanf("%d",&n)!=EOF){
cost = ;
int sum = ;
memset(mp,,sizeof(mp));
memset(vis,,sizeof(vis));
for(int i=;i<=n;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mp[u][v] = w;
sum+=w;
mp[v][u] = -w;
}
dfs(,-);
cost=-cost;
printf("%d\n",min(sum-cost,cost));
}
return ;
}

csu 1930 roads(DFS)的更多相关文章

  1. Codeforces Round #369 (Div. 2) D. Directed Roads dfs求某个联通块的在环上的点的数量

    D. Directed Roads   ZS the Coder and Chris the Baboon has explored Udayland for quite some time. The ...

  2. CodeForces #369 div2 D Directed Roads DFS

    题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...

  3. codeforces 711D D. Directed Roads(dfs)

    题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...

  4. Codeforces Round #369 (Div. 2) D. Directed Roads (DFS)

    D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input standard i ...

  5. Codeforces Round #369 (Div. 2) D. Directed Roads —— DFS找环 + 快速幂

    题目链接:http://codeforces.com/problemset/problem/711/D D. Directed Roads time limit per test 2 seconds ...

  6. lightoj 1049 - One Way Roads(dfs)

    Time Limit: 0.5 second(s) Memory Limit: 32 MB Nowadays the one-way traffic is introduced all over th ...

  7. CSU 1660 K-Cycle(dfs判断无向图中是否存在长度为K的环)

    题意:给你一个无向图,判断是否存在长度为K的环. 思路:dfs遍历以每一个点为起点是否存在长度为k的环.dfs(now,last,step)中的now表示当前点,last表示上一个访问的 点,step ...

  8. POJ 3411 Paid Roads(DFS)

    题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...

  9. Codeforces 711 D. Directed Roads (DFS判环)

    题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...

随机推荐

  1. send和sendmsg性能测试【sendmsg和send的性能基本一样,并没有得到优化】

    1,摘要:测试send和sendmsg的性能,影响这两个函数性能的因素主要有发送的字节大小,增加循环次数,从100到10000000(千万)减少计算误差 2,基本信息cat /proc/cpuinfo ...

  2. Racket里的方括号

    Racket里的方括号 Racket编程指南 https://blog.csdn.net/chinazhangyong/article/category/7386082 来自于QQ群racket!(  ...

  3. 【刷题】BZOJ 4316 小C的独立集

    Description 图论小王子小C经常虐菜,特别是在图论方面,经常把小D虐得很惨很惨. 这不,小C让小D去求一个无向图的最大独立集,通俗地讲就是:在无向图中选出若干个点,这些点互相没有边连接,并使 ...

  4. 【BZOJ3733】[Pa2013]Iloczyn (搜索)

    [BZOJ3733][Pa2013]Iloczyn (搜索) 题面 BZOJ 题解 把约数筛出来之后,直接爆搜,再随便剪枝就过了. 最近一句话题解倾向比较严重 #include<iostream ...

  5. HGOI20180822 五校联考卷

    T1 [题目意思]给出下列程序片段,预测程序运行结果 输入文件为T(T<=200)组数据,每组数据有个n(n<=1014) 输出文件为T行,每行一个数据,表示fun(n)的值 simple ...

  6. BZOJ2322 [BeiJing2011]梦想封印 【set + 线性基】

    题目链接 BZOJ2322 题解 鉴于BZOJ2115,要完成此题,就简单得多了 对图做一遍\(dfs\),形成\(dfs\)树,从根到每个点的路径形成一个权值,而每个返祖边形成一个环 我们从根出发去 ...

  7. VS2017企业版本(安装包+key)+ .NET Reflector 9.0

    关于VS2017安装的一点扩充说明(15.5):http://www.cnblogs.com/dunitian/p/8051985.html Key激活无需断网 Visual Studio 2017 ...

  8. jquery生成二维码并实现图片下载

    1.引入jquery的两个js文件 <script src="../scripts/erweima/jquery-1.10.2.min.js"></script& ...

  9. Python奇思妙想(胡思乱想)

    1.一道简单习题引发的思考深坑(通过globals及字典推导式获取类实例化了哪些对象) 初衷就是为了打印如下的信息: 小明,10岁,男,最爱大保健小明,10岁,男,开车去东北小明,10岁,男,最爱大保 ...

  10. Linux 服务器上快速配置阿里巴巴 OPSX NTP服务

    编辑文件 "/etc/ntp.conf",根据情况修改文件内容为: 互联网上的服务器: driftfile /var/lib/ntp/drift pidfile /var/run/ ...