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. pyqt最小化学习

    # -*- coding: cp936 -*- #!/usr/bin/env python # -*- coding:utf-8 -*- from PyQt4 import QtCore, QtGui ...

  2. 设置Eclipse启动JDK

    打开eclipse安装目录下的eclipse.ini文件,将红色内容加入 -vm ../Java/jdk1.6.0_26/bin (或者指向具体目录:D:/software/jdk_1.8u91/bi ...

  3. Linux下Samba的配置

    前言: 为了实现windows 和 Linux以及其它操作系统之间的资源共享,软件商推出nfs 和samba两种解决方案.因为市场上缺乏象pc-nfs那样的client工具,使得Linux和windo ...

  4. BOOST 线程完全攻略 - 扩展 - 事务线程

    扩展threadtimermoduleexceptionsocket 什么叫事务线程 举个例子: 我们写一个IM客户端的登录子线程,则该子线程会有这么几个事务要处理 No.1 TCP Socket物理 ...

  5. LR使用Java User协议环境报错Please add the <JDK>\bin to the path and try again

    看标题报错信息就知道,这是java编译及运行环境配置问题,运行LR脚本时,LR代理找不到java的JDK环境,当然,可能有人会遇到说,我在cmd窗口javac 环境是没问题的呀,是的,这就要看你的jd ...

  6. System.Speech.Synthesis 添加暂停、继续功能

    为了方便调用暂停.继续的方法.要将speech的功能写成一个类.直接附上代码: using System; using System.Collections.Generic; using System ...

  7. (转)Eclipse Shortcuts

    原文地址: http://javapapers.com/core-java/eclipse-shortcuts/ Editors are an integral part of a programme ...

  8. 1、Servlet 2、ServletConfig 3、ServletContext 4、HttpUrlConnection

    1.Servlet 2.ServletConfig 3.ServletContext 4.HttpUrlConnection 07. 五 / J2EE / 没有评论   一.第一个Servlet的编写 ...

  9. css布局学习笔记之box-sizing

    当你设置了元素的宽度,实际展现的元素却能够超出你的设置:因为元素的边框和内边距会撑开元素. .div{ width: 500px; margin: 20px auto; padding: 50px; ...

  10. javascript统计输入文本的简易方法

    计算文本框的输入字符数的简易方法: ]; var tValue = text.value; num = Math.ceil(getLength(tValue)/); //正则:用于区分中文为两个字节 ...