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. Spring Boot简单环境搭建

    #### 一.创建一个简单的Maven项目 使用`Maven`,通过导入`Spring Boot`的`starter`模块,可以将许多程序依赖的包自动导入到工程中.使用`Maven`的`parent ...

  2. 数字麦克风PDM信号采集与STM32 I2S接口应用(二)

    在使用STM32的数字麦克风I2S接口时,计算采样率让人头疼,芯片手册上没有明确的说法,而手册上的计算方法经过测试确和实验不符.借助搜索引擎,大部分资料都是来自于开发板卖家或开发板论坛,主要是咪头采集 ...

  3. Unity经典游戏教程之:弓之骑士

    版权声明: 本文原创发布于博客园"优梦创客"的博客空间(网址:http://www.cnblogs.com/raymondking123/)以及微信公众号"优梦创客&qu ...

  4. MySQL一键生成实体文件的神器-ginbro

    Java转过来的同学对Mybatis的使用肯定不陌生,特别是对一堆表去生成相应的dao和entity的时候使用Mybatis generator所带来的感触,无比深刻.前面我们也讲过原生的数据库使用, ...

  5. ccf 201903-3 损坏的RAID5

    9月份考ccf,暑假打算做一些往年的真题... 这个题,一开始真是把我给看晕了 传说中的大模拟,果然不简单QAQ 首先读懂题目就是一个大难点,特别是对于我这种题目一长就看不进去的人来说 读懂题目之后, ...

  6. ssm执行流程

    SSM运行流程 1:服务器启动,创建springmvc的前端控制器DispatcherServlet,创建Spring容器对象. 加载spring-servlet.xml .applicationCo ...

  7. java Timer工具类实现定时器任务

    第一 schedule 方法 三个参数 按照顺序 (执行的任务方法,开始执行时间,多少时间后循环去执行)  代码可用 public class TestScheedule { public stati ...

  8. 物流运输trans「ZJOI2006」

    [题目描述] 物流公司要把一批货物从码头\(A\)运到码头\(B\).由于货物量比较大,需要\(n\)天才能运完.货物运输过程中一般要转停好几个码头.物流公司通常会设计一条固定的运输路线,以便对整个运 ...

  9. Netty源码分析--内存模型(上)(十一)

    前两节我们分别看了FastThreadLocal和ThreadLocal的源码分析,并且在第八节的时候讲到了处理一个客户端的接入请求,一个客户端是接入进来的,是怎么注册到多路复用器上的.那么这一节我们 ...

  10. Elasticsearch Lucene 数据写入原理 | ES 核心篇

    前言 最近 TL 分享了下 <Elasticsearch基础整理>https://www.jianshu.com/p/e8226138485d ,蹭着这个机会.写个小文巩固下,本文主要讲 ...