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条边的有向图,可以把一条边反向,现在问有多少种方式可以使这个图没有环. 每个连 ...
随机推荐
- 初识elasticsearch_1(基本概念和基本操作)
初识 ElasticSearch是一个基于Lucene的搜索服务器,它提供了一个分布式多用户能力的全文搜索引擎,基于RESTful web接口.本博客部分基于es的官方文档.es的官方文档网址如下:h ...
- FMDB基本操作
1.以前使用数据库,因为一般就建立一张表,所以都是自己写代码创建,没用过fmdb,这次因为项目中涉及聊天模块,需要多张表格和数据库保存聊天记录 按照以前方法不好操作,就研究了下fmdb,发现确实挺方便 ...
- 轻松学JVM
轻松学JVM(一)——基本原理 前言 JVM一直是java知识里面进阶阶段的重要部分,如果希望在java领域研究的更深入,则JVM则是如论如何也避开不了的话题,本系列试图通过简洁易读的方式,讲解JVM ...
- BZOJ4589 Hard Nim(博弈+FWT)
即使n个数的异或为0.如果只有两堆,将质数筛出来设为1,做一个异或卷积即可.显然这个东西满足结合律,多堆时直接快速幂.可以在点值表示下进行. #include<iostream> #inc ...
- BZOJ4912 [Sdoi2017]天才黑客 【虚树 + 最短路】
题目链接 BZOJ4912 题解 转移的代价是存在于边和边之间的 所以把边看做点,跑最短路 但是这样做需要把同一个点的所有入边和所有出边之间连边 \(O(m^2)\)的连边无法接受 需要优化建图 膜一 ...
- 【bzoj2875】 Noi2012—随机数生成器
http://www.lydsy.com/JudgeOnline/problem.php?id=2875 (题目链接) 题意 求${X_{n}}$. Solution 矩乘板子,这里主要讲下会爆lon ...
- PostgreSQL(一)教程 -----SQL语言
一.概念 PostgreSQL是一种关系型数据库管理系统 (RDBMS).这意味着它是一种用于管理存储在关系中的数据的系统.关系实际上是表的数学术语. 今天,把数据存储在表里的概念已经快成了固有的常识 ...
- 【CF884D】Boxes And Balls k叉哈夫曼树
题目大意:给定一个大小为 N 的集合,每次可以从中挑出 2 个或 3 个数进行合并,合并的代价是几个数的权值和,求将这些数合并成 1 个的最小代价是多少. 引理:K 叉哈夫曼树需要保证 \((n-1) ...
- Spark记录-Scala循环语句
Scala while循环语句 当给定条件为真时,while循环重复一个语句或一组语句.它在执行循环体之前测试条件状态. 只要给定的条件为真,while循环语句重复执行目标语句. object Dem ...
- Dubbo学习笔记11:使用Dubbo中需要注意的一些事情
指定方法异步调用 前面我们讲解了通过设置ReferenceConfig的setAsync()方法来让整个接口里的所有方法变为异步调用,那么如何指定某些方法为异步调用呢?下面讲解下如何正确地设置默写方法 ...