csu 1930 roads(DFS)
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)的更多相关文章
- 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 ...
- CodeForces #369 div2 D Directed Roads DFS
题目链接:D Directed Roads 题意:给出n个点和n条边,n条边一定都是从1~n点出发的有向边.这个图被认为是有环的,现在问你有多少个边的set,满足对这个set里的所有边恰好反转一次(方 ...
- codeforces 711D D. Directed Roads(dfs)
题目链接: D. Directed Roads time limit per test 2 seconds memory limit per test 256 megabytes input stan ...
- 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 ...
- 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 ...
- 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 ...
- CSU 1660 K-Cycle(dfs判断无向图中是否存在长度为K的环)
题意:给你一个无向图,判断是否存在长度为K的环. 思路:dfs遍历以每一个点为起点是否存在长度为k的环.dfs(now,last,step)中的now表示当前点,last表示上一个访问的 点,step ...
- POJ 3411 Paid Roads(DFS)
题目链接 点和边 都很少,确定一个界限,爆搜即可.判断点到达注意一下,如果之前已经到了,就不用回溯了,如果之前没到过,要回溯. #include <cstring> #include &l ...
- Codeforces 711 D. Directed Roads (DFS判环)
题目链接:http://codeforces.com/problemset/problem/711/D 给你一个n个节点n条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
随机推荐
- 深入理解Java之线程池(爱奇艺面试)
爱奇艺的面试官问 (1) 线程池是如何关闭的 (2) 如何确定线程池的数量 一.线程池销毁,停止线程池 ThreadPoolExecutor提供了两个方法,用于线程池的关闭,分别是shutdown() ...
- Tether USDT 节点钱包的安装与使用
当前,在进行数字资产交易的过程中,由于各国政府的政策因素,法币成为数字资产交易的一个重要问题.在法币接入数字资产交易的过程中,通常是用某种数字资产对法币进行锚定,例如bitshares上面的许多b ...
- python--inspect模块
inspect模块主要提供了四种用处: 1.对是否是模块.框架.函数进行类型检查 2.获取源码 3.获取类或者函数的参数信息 4.解析堆栈 一.type and members 1. inspect. ...
- Chemical table CodeForces - 1012B
题意: 一个棋盘 对于任何一个棋盘中的矩形 如果 任意三角存在棋子 则第四个角会自动生成一个棋子 求铺满整个棋盘 我们至少要向棋盘里加多少枚棋子 解析: 这题就是求图中有多少个连通图,可以直接dfs ...
- 解题:SPOJ 422 Transposing is Even More Fun
题面 这种换来换去的东西很容易想到置换群那一套,然后题目甚至还暗示了二进制=.= 直接换的话显然是$2^{a+b}$次,但是一个循环节里可以少换一次,然后问题就变成了数循环节 在一个循环节里的位置有什 ...
- 解题:THUWC 2017 在美妙的数学王国中畅游
题面 _“数字和数学规律主宰着这个世界.”_ 在 @i207M 帮助下折腾了半天终于搞懂了导数和泰勒展开,引用某学长在考场上的感受:感觉整个人都泰勒展开了 显然是个奇奇怪怪的东西套上LCT,发现直接维 ...
- Luogu 1613 跑路(最短路径,倍增)
Luogu 1613 跑路(最短路径,倍增) Description 小A的工作不仅繁琐,更有苛刻的规定,要求小A每天早上在6:00之前到达公司,否则这个月工资清零.可是小A偏偏又有赖床的坏毛病.于是 ...
- 【洛谷P2114】起床困难综合征 位运算+贪心
题目大意:给定 N 个操作,每个操作为按位与.或.异或一个固定的数字,现在要求从 0 到 M 中任选一个数字,使得依次经过 N 个操作后的值最大. 题解:位运算有一个重要的性质是:位运算时,无进位产生 ...
- ElasticStack系列之十九 & bulk时 index 和 create 的区别
区别: 两篇文章 id 都一样的情况下,index 是将第二篇文章覆盖第一篇:create 是在第二篇插入的时候抛出一个已经存在的异常 解释: 在批量请求的时候最好使用 create 方式进行导入.假 ...
- Angular.js浅谈
至今博主对于MVVM框架比较了解的就只能算有Angular了,首先给大家明确一个概念,Angular1.x才能叫Angular.js,而Angular2.4.5都直接叫Angular了,因为从2开始已 ...