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条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
随机推荐
- IntelliJ IDEA之如何提交代码到SVN服务器
一.准备 参照<IntelliJ IDEA教程之如何配置SVN>这篇文章配置SVN插件.提交代码到SVN之前,记得要先创建版本库,请参照<<SVN如何建立版本库>> ...
- 51nod 1463 找朋友 (扫描线+线段树)
1463 找朋友 基准时间限制:1.5 秒 空间限制:262144 KB 分值: 80 难度:5级算法题 收藏 关注 给定: 两个长度为n的数列A .B 一个有m个元素的集合K 询问Q次 每次询 ...
- AtCoder Grand Contest 008
AtCoder Grand Contest 008 A - Simple Calculator 翻译 有一个计算器,上面有一个显示按钮和两个其他的按钮.初始时,计算器上显示的数字是\(x\),现在想把 ...
- 【洛谷P1101】单词方阵
题目大意:给一 \(n \times n\) 的字母方阵,内可能蕴含多个 \("yizhong"\) 单词.单词在方阵中是沿着同一方向连续摆放的.摆放可沿着 8 个方向的任一方向, ...
- MySQL的replace方法
mysql中replace函数直接替换mysql数据库中某字段中的特定字符串,不再需要自己写函数去替换,用起来非常的方便,mysql 替换函数replace()Update `table_name` ...
- python urllib和urllib3包使用(转载于)
urllib.request 1. 快速请求 2.模拟PC浏览器和手机浏览器 3.Cookie的使用 4.设置代理 urllib.error URLError HTTPError urllib.par ...
- map文件的使用
map文件相信大家并不陌生,大家都知道是用来调试的,但是具体怎么用你又清不清楚呢? 其实也很简单 1.拿JQ为例,我们需要备有jquery.js.jquery.min.js.jquery.min.ma ...
- Oracle DBMS_RANDOM
DBMS_RANDOM.VALUE -- [0,1)的38位精度小数 SELECT DBMS_RANDOM.VALUE FROM DUAL; -- [10,20)的38位精度小数 SELECT DBM ...
- Hadoop基础-常见异常剖析之防坑小技巧
Hadoop基础-常见异常剖析之防坑小技巧 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任.
- SQL记录-PLSQL包
PL/SQL包 PL/SQL包是组逻辑相关的PL/SQL类型,变量和子程序模式对象. 程序包将有两个强制性的部分: 包装规范定义 包体或定义 包装规范定义 规范是接口到包.它只是声明的类型,变量,常量 ...