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 (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) - 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(搜索)的更多相关文章
- POJ 1724 ROADS【最短路/搜索/DP】
一道写法多样的题,很具有启发性. 具体参考:http://www.cnblogs.com/scau20110726/archive/2013/04/28/3050178.html http://blo ...
- poj 1724:ROADS(DFS + 剪枝)
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 10777 Accepted: 3961 Descriptio ...
- ACM: 强化训练-Roads in the North-BFS-树的直径裸题
Roads in the North Time Limit:1000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu De ...
- 多次访问节点的DFS POJ 3411 Paid Roads
POJ 3411 Paid Roads Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 6553 Accepted: 24 ...
- [USACO2002][poj1947]Rebuilding Roads(树形dp)
Rebuilding RoadsTime Limit: 1000MS Memory Limit: 30000KTotal Submissions: 8589 Accepted: 3854Descrip ...
- 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 ...
- ROADS
ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 11977 Accepted: 4429 Description N ...
- HDU 1242 Rescue (BFS(广度优先搜索))
Rescue Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submis ...
- poj 3249 Test for Job (DAG最长路 记忆化搜索解决)
Test for Job Time Limit: 5000MS Memory Limit: 65536K Total Submissions: 8990 Accepted: 2004 Desc ...
随机推荐
- 关于safari上的select宽高问题小技,自定义下拉框
之前一直用windows做开发,最近换了个mac,在几经折腾之下,安装完了各种开发工具,IDE等,然后欣然打开自己正在开发的网站.突然发现mac上所有的下拉框都变了,都是默认样式,无论padding, ...
- (转) unity 在移动平台中,文件操作路径详解
http://www.unitymanual.com/thread-23491-1-1.html 今天,这篇文章其实是个老生常谈的问题咯,在网上类似的文章也比比皆是,在此我只是做个详细总结方便大家能够 ...
- Error creating bean with name 'memcachedClient'...java.lang.OutOfMemoryError
1,Tomcat启动报错例如以下: Caused by: org.springframework.beans.factory.BeanCreationException: Error creating ...
- boost库asio详解1——strand与io_service区别
namespace { // strand提供串行执行, 能够保证线程安全, 同时被post或dispatch的方法, 不会被并发的执行. // io_service不能保证线程安全 boost::a ...
- Query获取值常用
Query获取Select选择的Text和Value:语法解释:1. $("#select_id").change(function(){//code...}); //为Sel ...
- 面试前的准备---C#知识点回顾----04
播下的种子,慢慢开始发芽收获了,陆陆续续offer就来了,该轮到我挑的时候了 今天面试的一家公司,技术问的相对宽广和细致,程度令人发指 1.谈谈ViewState 这个问题,回答的好,工资翻一级 基本 ...
- 推荐几本不错的ASP.NET MVC书
以前主要是做PHP应用的,由于工作需要,捡起来.NET, 特别是新技术层出不穷,找了几本书看,个人感觉还不错,网上也有电子版的下载 一. ASP.NET MVC4 Web 编程 O'Reilly出版社 ...
- solr group分组查询
如:http://localhost:8080/solr/test_core/select?q=*:*&wt=json&indent=true&group=true&g ...
- IOS开发之程序执行状态更改
1 前言 上节我们介绍了程序执行的状态,从例子中我们可以发现处理这些状态更改的时候有明确的策略可以遵循,这次我们就来介绍一下. 2 详述 2.1 活动->不活动 使用applicationWil ...
- No1_4.数组的基本操作_Java学习笔记
import java.util.Arrays; public class HelloArrayOp { public static void main(String[] args) { // TOD ...