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. ios开发--给应用添加新的字体的方法

    1.网上搜索字体文件(后缀名为.ttf,或.odf) 2.把字体库导入到工程的resouce中 3.在程序添加以下代码 输出所有字体 NSArray *familyNames = [UIFont fa ...

  2. Linux 文件系统相关的基本概念

    本文介绍 Linux 文件系统相关的基本概念. 硬盘的物理组成 盘片硬盘其实是由单个或多个圆形的盘片组成的,按照盘片能够容纳的数据量,分为单盘(一个硬盘里面只有一个盘片)或多盘(一个硬盘里面有多个盘片 ...

  3. python 的几种数据类型

    列表  列表是 Python  的主力数据类型.当提到 " 列表 " 时,您脑海中可 能会闪现" 必须进一步声明大小的数组,只能包含同一类对象 "  等想法.千 ...

  4. 对JAVA Bean使用PropertyDescriptor反射调用JAVA方法低耦合

    对于符合JAVA Bean规范的bean,调用其方法应优先使用java.beans.PropertyDescriptor获取Method进行方法调用,以获得更大的可维护性. public void g ...

  5. 【POJ - 3258】River Hopscotch(二分)

    River Hopscotch 直接中文 Descriptions 每年奶牛们都要举办各种特殊版本的跳房子比赛,包括在河里从一块岩石跳到另一块岩石.这项激动人心的活动在一条长长的笔直河道中进行,在起点 ...

  6. 配置VNC并远程控制服务器(电脑)

    先象征性介绍一下: VNC (Virtual Network Console)是虚拟网络控制台的缩写, 它是一款基于 UNIX 和 Linux 操作系统的优秀.免费.开源的远程控制工具软件. 然后开始 ...

  7. 初试kafka消息队列中间件一 (只适合初学者哈)

    初试kafka消息队列中间件一 今天闲来有点无聊,然后就看了一下关于消息中间件的资料, 简单一点的理解哈,网上都说的太高大上档次了,字面意思都想半天: 也就是用作消息通知,比如你想告诉某某你喜欢他,或 ...

  8. Canvas动画(PC端 移动端)

    Canvas动画(PC端 移动端) 一,介绍与需求 1.1,介绍 canvas是HTML5中新增一个HTML5标签与操作canvas的javascript API,它可以实现在网页中完成动态的2D与3 ...

  9. Paxos算法原理

    1.从ACID到CAP 我们知道传统集中式系统中实现ACID是很简单的,在分布式环境中,涉及到不同的节点,节点内的ACID可以控制,那么节点间的ACID如何控制呢?构建一个可用性和一致性的分布系统成为 ...

  10. mysql的引擎问题,主键和外键的创建问题,以及创建外键不成功,却创建了一个索引

    mysql的引擎问题: 需要知道的三个引擎:InnoDB--是一个事务处理引擎,不支持全文检索,支持事务操作,即DML操作: Memory--是一个数据存储在内存,速度很快,功能上等同于MyIsam, ...