Time Limit: 0.5 second(s) Memory Limit: 32 MB

Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Dhaka Division decided to keep up with new trends. Formerly all n cities of Dhaka were connected by n two-way roads in the ring, i.e. each city was connected directly to exactly two other cities, and from each city it was possible to get to any other city. Government of Dhaka introduced one-way traffic on all n roads, but it soon became clear that it's impossible to get from some of the cities to some others. Now for each road is known in which direction the traffic is directed at it, and the cost of redirecting the traffic. What is the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other?

Input

Input starts with an integer T (≤ 200), denoting the number of test cases.

Each case starts with a blank line and an integer n (3 ≤ n ≤ 100) denoting the number of cities (and roads). Next n lines contain description of roads. Each road is described by three integers ai, bi, ci (1ai,bin,aibi,1ci100) - road is directed from city ai to city bi, redirecting the traffic costs ci.

Output

For each case of input you have to print the case number and the smallest amount of money the government should spend on the redirecting of roads so that from every city you can get to any other.

题意:给出n个点, n条边, n条边把n个点组成一个”环”, 但是,有些边方向不对, 导致某些点无法到达其他点,现在告诉你每条边修改方向的代价, 问把n个点组成一个真正的环,使得每个点都可以到达其他任何点的代价是多少。

数据不大,简单的搜索一遍即可,一道简单的dfs

#include <iostream>
#include <cstring>
using namespace std;
const int inf = 0X3f3f3f3f;
int map[110][110] , vis[110];
int sum , n;
void dfs(int s , int t , int val , int step) {
if(s == t && step == n) {
sum = min(sum , val);
return ;
}
for(int i = 1 ; i <= n ; i++) {
if(vis[i] != 1) {
if(map[t][i] == 0 && map[i][t] != 0) {
vis[i] = 1;
dfs(s , i , val + map[i][t] , step + 1);
vis[i] = 0;
}
if(map[t][i] != 0) {
vis[i] = 1;
dfs(s , i , val , step + 1);
vis[i] = 0;
}
}
}
}
int main()
{
int t;
cin >> t;
int ans = 0;
while(t--) {
ans++;
cin >> n;
for(int i = 0 ; i <= n ; i++) {
for(int j = 0 ; j <= n ; j++) {
map[i][j] = 0;
}
}
for(int i = 0 ; i < n ; i++) {
int x , y , z;
cin >> x >> y >> z;
map[x][y] = z;
}
memset(vis , 0 , sizeof(vis));
sum = inf;
dfs(1 , 1 , 0 , 0);
cout << "Case " << ans << ": " << sum << endl;
}
return 0;
}

lightoj 1049 - One Way Roads(dfs)的更多相关文章

  1. 1049 - One Way Roads 观察 dfs

    http://lightoj.com/volume_showproblem.php?problem=1049 题意是,在一副有向图中,要使得它变成一个首尾相连的图,需要的最小代价. 就是本来是1--& ...

  2. 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 ...

  3. CodeForces #369 div2 D Directed Roads DFS

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

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

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

  5. 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 ...

  6. csu 1930 roads(DFS)

    Description Once upon a time there was a strange kingdom, the kingdom had n cities which were connec ...

  7. 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 ...

  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. LongAdder和AtomicLong性能对比

    jdk1.8中新原子操作封装类LongAdder和jdk1.5的AtomicLong和synchronized的性能对比,直接上代码: package com.itbac.cas; import ja ...

  2. 对API进行版本控制的重要性和实现方式

    我在API设计中收到的最常见问题之一就是如何对API进行版本控制.虽然并非所有API都完全相同,但我发现在API版本控制方面,某些模式和实践适用于大多数团队.我已经将这些内容收集起来,下面将提供一些关 ...

  3. kubernetes集群升级的正确姿势

    kubernetes社区非常活跃,每季度都会发布一个release.但是线上集群业务可用性要求较高,场景复杂,任何微小的变更都需要非常小心,此时跟随社区版本进行升级略显吃力.但是为了能够使用到最新的一 ...

  4. Why do I write a blog

    I believe the most beautiful and elegant answer to this question is from Churchill. "On a peace ...

  5. virtualbox安装ubuntu16 LTS及其配置

    一.下载安装VirtualBox 1. 从官网下载VirtualBox,目前版本:VirtualBox 6.0.6 for Windows hosts x86/amd64 2. 下载好之后安装Virt ...

  6. Go中配置文件读取的几种方式

    日常开发中读取配置文件包含以下几种格式: json 格式字符串 K=V 键值对 xml 文件 yml 格式文件 toml 格式文件 前面两种书写简单,解析过程也比较简单.xml形式书写比较累赘,yml ...

  7. 基于RobotFramework实现自动化测试

    Java + robotframework + seleniumlibrary 使用Robot Framework Maven Plugin(http://robotframework.org/Mav ...

  8. 2019最新最全Java开发面试常见问题答案总结

    2019最新最全Java开发面试常见问题答案总结 马上准备9月份出去面试Java开发,自己学习丢西瓜捡芝麻,学了的都忘了,所以有机会自己做个学习笔记,摘录自各个博文以及总结. 1.JAVA面向对象的特 ...

  9. Linux文件及目录管理

    1.Linux文件目录树 /:根目录,linux文件系统的最顶端和入口 bin:存放用户二进制文件(如:ls,cd,mv等),实则/user/bin的硬链接(相当于Windows系统的快捷方式) bo ...

  10. 算法之《图》Java实现

    数据结构之图 定义(百度百科) 图的术语表 无向图 深度优先搜索 广度优先遍历 有向图 路径问题 调度问题 强连通性 最小生成树(无向图) 最小生成树的贪心算法 加权无向图的数据结构 Kruskal算 ...