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 ...
随机推荐
- web前端之 JS
JavaScript概述 JavaScript是一门编程语言,简称js,由浏览器编译并运行,JS说白了就是让页面能够动起来 js存在形式 1.在html页面中 <script> alert ...
- Android进程间通信(IPC)机制Binder简要介绍和学习计划
文章转载至CSDN社区罗升阳的安卓之旅,原文地址:http://blog.csdn.net/luoshengyang/article/details/6618363 在Android系统中,每一个应用 ...
- HttpServletRequest对象请求转发和HttpServletResponse对象请求重定向之间的区别
HttpServletRequest对象request代表浏览器请求对象,HttpServletResponse对象代表服务器响应对象,当用浏览器访问web服务器,发出请求时,Servlet调用ser ...
- NET基础课-- 类型基础(NET之美)
1.类型:值类型 引用类型. 分类依据:类型在内存的分配方式.值类型在堆栈,引用类型在托管堆. 名词:栈--所有变量都会被分配在栈上,只不过值类型直接含有数据,引用类型含有一个指向托管堆对象的地址. ...
- Android应用去掉标题栏的方法
1.在代码里实现 this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏,this指当前的Activity 这句代码一定要加在setCon ...
- SQL Server 模式和名称解析
模式实际上是名称空间,因此在SQL Serve中调用数据库对象时,一定要设置对象引用的环境.每个用户都被赋予了一个默认模式,在用户登录SQL Server并凋用数据库对象时,这个默认模式就是对象引用方 ...
- python单元测试--深入理解unittest
单元测试的重要性就不多说了,可恶的是python中有太多的单元测试框架和工具,什么unittest, testtools, subunit, coverage, testrepository, nos ...
- mysql dos启动出现1067错误的解决方法
请参看下面的链接:http://www.webjx.com/htmldata/2007-10-16/1192542247.html
- 查找mysql数据库文件的存放位置
在mysql数据库中,有时候并不是很容易找出mysql数据库文件data的存放位置吗,这时就可以使用mysql自带的命令行工具进行查询. 具体命令如下:show variables like '%da ...
- TextView之一:子类的常用属性
TextView常见的子类包括EditText,Button,CheckBox, RadioButton等. 1.EditText EditText继承自TextView,因此TextView所有属性 ...