One Way Roads

Time Limit:500MS     Memory Limit:32768KB     64bit IO Format:%lld & %llu

Description

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 nroads, 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). Nextn 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.

Sample Input

4

3

1 3 1

1 2 1

3 2 1

3

1 3 1

1 2 5

3 2 1

6

1 5 4

5 3 8

2 4 15

1 6 16

2 3 23

4 6 42

4

1 2 9

2 3 8

3 4 7

4 1 5

Sample Output

Case 1: 1

Case 2: 2

Case 3: 39

Case 4: 0

题解:单向图,问加入边,求花费的最小价钱;

保证每个点连两个点;

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
#include<map>
#include<vector>
#include<set>
using namespace std;
const int INF = 0x3f3f3f3f;
#define mem(x, y) memset(x, y, sizeof(x))
typedef long long LL;
void SI(int &x){scanf("%d", &x);}
void SI(LL &x){scanf("%lld", &x);}
int mp[110][110];
vector<int>vec[110];
int vis[110];
int ans;
int n;
int dot[110];
int tp;
void dfs(int u){
for(int i = 0; i < vec[u].size(); i++){
int v = vec[u][i];
if(vis[v])continue;
dot[++tp] = v;
vis[v] = 1;
dfs(v);
}
}
int main(){
int T, a, b, c, kase = 0;
scanf("%d", &T);
while(T--){
scanf("%d", &n);
mem(mp,0);
mem(vis,0);
for(int i = 0; i < 110; i++)vec[i].clear();
for(int i = 0; i < n; i++){
scanf("%d%d%d", &a, &b, &c);
mp[a][b] = c;
vec[a].push_back(b);
vec[b].push_back(a);
}
tp = 0;
dot[++tp] = 1;
vis[1] = 1;
dfs(1);
int ans1 = 0, ans2 = 0;
for(int i = 2; i <= tp; i++){
int u = dot[i - 1], v = dot[i];
//printf("u = %d v = %d\n", u, v);
if(mp[u][v] == 0){
ans1 += mp[v][u];
}
}
if(mp[dot[tp]][dot[1]] == 0)
ans1 += mp[dot[1]][dot[tp]];
//printf("ans1 = %d\n", ans1);
for(int i = tp; i >= 2; i--){
int u = dot[i], v = dot[i - 1];
// printf("u = %d v = %d\n", u, v);
if(mp[u][v] == 0){
ans2 += mp[v][u];
}
}
if(mp[dot[1]][dot[tp]] == 0)
ans2 += mp[dot[tp]][dot[1]];
//printf("ans2 = %d\n", ans2);
printf("Case %d: %d\n", ++kase, min(ans1, ans2));
}
return 0;
}

One Way Roads(搜索)的更多相关文章

  1. POJ 1724 ROADS【最短路/搜索/DP】

    一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...

  2. poj 1724:ROADS(DFS + 剪枝)

    ROADS Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 10777   Accepted: 3961 Descriptio ...

  3. ACM: 强化训练-Roads in the North-BFS-树的直径裸题

    Roads in the North Time Limit:1000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu De ...

  4. 多次访问节点的DFS POJ 3411 Paid Roads

    POJ 3411 Paid Roads Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 6553   Accepted: 24 ...

  5. [USACO2002][poj1947]Rebuilding Roads(树形dp)

    Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...

  6. HDU 4900 NO ACM NO LIFE(概率+枚举+搜索)(2014 Multi-University Training Contest 4)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4900 Problem Description There is an old country and ...

  7. ROADS

    ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11977 Accepted: 4429 Description N ...

  8. HDU 1242 Rescue (BFS(广度优先搜索))

    Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...

  9. poj 3249 Test for Job (DAG最长路 记忆化搜索解决)

    Test for Job Time Limit: 5000MS   Memory Limit: 65536K Total Submissions: 8990   Accepted: 2004 Desc ...

随机推荐

  1. Hibernate框架大配置关联小配置

    1 <?xml version='1.0' encoding='utf-8'?> <!DOCTYPE hibernate-configuration PUBLIC "-// ...

  2. Deep Compression Compressing Deep Neural Networks With Pruning, Trained QuantizationAnd Huffman Coding

    转载请注明出处: http://www.cnblogs.com/sysuzyq/p/6200613.html by 少侠阿朱

  3. GridView动态构建OrderBy进行排序

    废话不说,直接上例子: 前台代码: <asp:GridView ID="GridView1" runat="server" AllowSorting=&q ...

  4. C++中malloc/free和new/delete 的使用

    malloc/free 的使用要点 函数malloc的原型如下: void * malloc(size_t size); 用malloc申请一块长度为length的整数类型的内存,程序如下: int ...

  5. 3. QT窗体间值的传递(续)

    一.前言 上篇博客中通过重载子窗体的构造函数将主窗体的值传入到子窗体,但是在子窗体运行过程中如何才能将值动态的传入到子窗体?可以有两种办法,1.信号和槽的方式传值:2.主窗体中将传出值设置为publi ...

  6. 初识_IOS-简易计算器-问题总结

    作为一个新手,只好拿所有开发者都写的不想写又没有太大难度的计算器来下手咯.比较细一点,耐心哟. 我们都知道,计算器首要任务就是计算,那我们就直接看成A+B=C,来进行分析了.对A,B,C三个对象进行分 ...

  7. C# WebForm 使用NPOI 2 生成简单的word文档(.docx)

    使用NPOI可以方便的实现服务端对Word.Excel的读写.要实现对Word的读写操作,需要引用NPOI.OOXML.dll,应用命名空间XWPF. 本文使用NPOI 2.0实现对Word的基本生成 ...

  8. iOS-OC-基础-NSDictionary常用方法

    /*=============================NSDictionary(不可变字典)的使用=========================*/ //————————————————— ...

  9. javaScript动态添加样式

    [动态添加css样式] <html> <head> <script type="text/javascript"> window.onload= ...

  10. TRI 解题报告

    题目大意: 在一个平面上有N(N <= 1000)个点,其中任意三点不共线,求这些点组成的三角形的面积和每和三角形内部含的点数的个数和. 数据范围: 20%的数据 N <= 50, 30% ...