CodeForces 24A Ring road(dfs)

8 篇文章 0 订阅
A. Ring road
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
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?

Input

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 aibici(1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 100) — road is directed from city ai to city bi, redirecting the traffic costs ci.

Output

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.

Examples
input
3
1 3 1
1 2 1
3 2 1
output
1
input
3
1 3 1
1 2 5
3 2 1
output
2
input
6
1 5 4
5 3 8
2 4 15
1 6 16
2 3 23
4 6 42
output
39
input
4
1 2 9
2 3 8
3 4 7
4 1 5
output
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;

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值