CodeForces 24A Ring road(dfs)
2 seconds
256 megabytes
standard input
standard output
Nowadays the one-way traffic is introduced all over the world in order to improve driving safety and reduce traffic jams. The government of Berland decided to keep up with new trends. Formerly all ncities
of Berland 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 Berland 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?
The first line contains integer n (3 ≤ n ≤ 100)
— amount of cities (and roads) in Berland. Next nlines 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 single integer — 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.
3
1 3 1
1 2 1
3 2 1
1
3
1 3 1
1 2 5
3 2 1
2
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
39
4
1 2 9
2 3 8
3 4 7
4 1 5
0
深搜
#include <iostream>
#include <string.h>
#include <stdlib.h>
#include <algorithm>
#include <math.h>
#include <stdio.h>
#include <vector>
using namespace std;
vector<pair<int,int> >a[105];
vector<pair<int,int> >b[105];
int n;
int num;
int now;
void dfs(int x,int pre)
{
if(x==now)
return;
if(a[x].size()>0&&a[x][0].first!=pre)
dfs(a[x][0].first,x);
else if(a[x].size()>1&&a[x][1].first!=pre)
dfs(a[x][1].first,x);
else if(b[x].size()>0&&b[x][0].first!=pre)
{
num+=b[x][0].second;
dfs(b[x][0].first,x);
}
else if(b[x].size()>1&&b[x][1].first!=pre)
{
num+=b[x][1].second;
dfs(b[x][1].first,x);
}
}
int main()
{
scanf("%d",&n);
int x,y,z; for(int i=1;i<=n;i++)
{
scanf("%d%d%d",&x,&y,&z);
a[x].push_back(make_pair(y,z));
b[y].push_back(make_pair(x,z));
}
int ans=1e9;
for(int i=1;i<=n;i++)
{
num=0;
now=i;
if(a[i].size()>0)
{
num=0,now=i;
dfs(a[i][0].first,i);
ans=min(ans,num);
}
if(a[i].size()>1)
{
num=0,now=i;
dfs(a[i][1].first,i);
ans=min(ans,num);
}
if(b[i].size()>0)
{
num=0,now=i;
num+=b[i][0].second;
dfs(b[i][0].first,i);
ans=min(ans,num);
}
if(b[i].size()>1)
{
num=0,now=i;
num+=b[i][1].second;
dfs(b[i][1].first,i);
ans=min(ans,num);
} }
printf("%d\n",ans);
return 0; }
CodeForces 24A Ring road(dfs)的更多相关文章
- CodeForces 27D - Ring Road 2 构图2-sat..并输出选择方案
题意 n个数1~n按顺序围成一个圈...现在在某些两点间加边..边可以加在圈内或者圈外..问是否会发生冲突?如果不发生冲突..输每一条边是放圈内还是圈外. 题解 ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- [Codeforces 1214D]Treasure Island(dfs)
[Codeforces 1214D]Treasure Island(dfs) 题面 给出一个n*m的字符矩阵,'.'表示能通过,'#'表示不能通过.每步可以往下或往右走.问至少把多少个'.'变成'#' ...
- [Codeforces 163D]Large Refrigerator (DFS+剪枝)
[Codeforces 163D]Large Refrigerator (DFS+剪枝) 题面 已知一个长方体的体积为V,三边长a,b,c均为正整数,求长方体的最小表面积S V以质因数分解的形式给出 ...
- Codeforces 839C Journey【DFS】
C. Journey time limit per test:2 seconds memory limit per test:256 megabytes input:standard input ou ...
- 【Codeforces 738C】Road to Cinema
http://codeforces.com/contest/738/problem/C Vasya is currently at a car rental service, and he wants ...
- Codeforces Gym 100463D Evil DFS
Evil Time Limit: 20 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/gym/100463/attachments Descr ...
- HDU 1016 Prime Ring Problem (DFS)
Prime Ring Problem Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Other ...
- Educational Codeforces Round 25 Five-In-a-Row(DFS)
题目网址:http://codeforces.com/contest/825/problem/B 题目: Alice and Bob play 5-in-a-row game. They have ...
随机推荐
- Release Management
1. Understand the current state of release management. test environment limitations how long to setu ...
- atitit.研发管理--标准化流程总结---java开发环境与项目部署环境的搭建工具包总结
atitit.研发管理--标准化流程总结---java开发环境与项目部署环境的搭建工具包总结 1. ide系列(只开发环境需要,但部署环境也做好放上,很有用) 1 2. web服务器+sdk+网站程序 ...
- 最新的Zynq资料整理
1.Zynq修炼秘籍 基于ZYNQ的FPGA基础入门 基于ZYNQ的Soc入门基础 基于ZYNQ的裸机应用开发 基于ZYNQ硬件的LINUX开发 基于ZYNQ的HLS图像算法设计 基于ZYNQ的图像处 ...
- 用swift开发仪表盘控件(二)
二.代码分析 这个控件本质就是从UIView继承的一个类而已.所以整个代码事实上就是一个定制的UIView类. 依据UIView的规则进行例如以下初始化: required init(coder aD ...
- Unix系统编程()信号类型和默认行为
信号类型和默认行为 就是讲了有多少个信号类型 好多啊,后面用到了再看...
- CSS布局奇淫技巧之--各种居中<转>
居中是我们使用css来布局时常遇到的情况.使用css来进行居中时,有时一个属性就能搞定,有时则需要一定的技巧才能兼容到所有浏览器,本文就居中的一些常用方法做个简单的介绍. 注:本文所讲方法除了特别说明 ...
- mf210v 端口的映射
ttyUSB0 : 诊断端口 ttyUSB1 : AT指令端口 ttyUSB2 : VoUSB端口(语音) ttyUSB3 : Modem端口
- 干接点&湿接点
干接点的定义: 无源开关:具有闭合和断开的2种状态:2个接点之间没有极性,可以互换: 常见的干接点信号有: 1.各种开关如:限位开关.行程开关.脚踏开关.旋转开关.温度开关.液位开关等: 2.各种按键 ...
- /proc/interrupts 和 /proc/stat 查看中断的情况
在/proc文件系统下,又两个文件提供了中断的信息. /proc/interrupts 文件中列出当前系统使用的中断的情况,所以某个中断处理没有安装,是不会显示的.哪怕之前安装过,被卸载了. 从左到右 ...
- MySQL5.0、5.1、5.5、5.6功能进化
目前线上使用的版本情况:新上线端口统一使用5.5,不说别的,一个快速恢复重启就值回票价. 但因为历史原因还有大量5.1的版本,甚至,I’am sorry,还有少数5.0的版本. 至于5.0以前的版本, ...