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. Spring之jdbcTemplate:增删改

    JdbcTemplate增删改数据操作步骤:1.导入jar包:2.设置数据库信息:3.设置数据源:4.调用jdbcTemplate对象中的方法实现操作 package helloworld.jdbcT ...

  2. cglib动态代理是通过继承父类的方式进行代理的 不是通过接口方式进行动态代理的 因此可以对普通的类进行代理

    cglib动态代理是通过继承父类的方式进行代理的 不是通过接口方式进行动态代理的

  3. java之不修改变量的数据类型的处理方式

  4. python学习大全:python基础进阶+人工智能+机器学习+神经网络

    首先用数据说话,看看资料大小,达到675G承诺:真实资料.不加密.(鉴于太多朋友加我QQ,我无法及时回复,) 方便的朋友给我点赞.评论下,谢谢!(内容较大,多次保存) [hide]链接:[url]ht ...

  5. 【刷题】BZOJ 2151 种树

    Description A城市有一个巨大的圆形广场,为了绿化环境和净化空气,市政府决定沿圆形广场外圈种一圈树.园林部门得到指令后,初步规划出n个种树的位置,顺时针编号1到n.并且每个位置都有一个美观度 ...

  6. Graham's Scan法求解凸包问题

    概念 凸包(Convex Hull)是一个计算几何(图形学)中的概念.用不严谨的话来讲,给定二维平面上的点集,凸包就是将最外层的点连接起来构成的凸多边型,它能包含点集中所有点的.严谨的定义和相关概念参 ...

  7. codeforces906 D

    题目链接:http://codeforces.com/contest/906/problem/D 题意: 给你n个数,再给你l~r,求%m 题解: 一开始不会 后来查到了欧拉降幂定理: 然后就会了 这 ...

  8. 2017年8道php面试题(真题+答案)

    1.<?php echo count(strlen(“http://php.net”)); ?>的执行结果是? 答案:1 .如果var是普通变量,则返回1.正常情况下返回var中的元素或属 ...

  9. Linux(Debian)软件安装

    # 配置/etc/apt/sources.list 通过root权限修改/etc/apt/sources.list $ su #输入密码进入root权限 $ chmod 0666 /etc/apt/s ...

  10. Linux - 磁盘操作

    Linux 磁盘常见操作 : df -Ph # 查看硬盘容量 df -T # 查看磁盘分区格式 df -i # 查看inode节点 如果inode用满后无法创建文件 du -h 目录 # 检测目录下所 ...