【codeforces24A】Ring road

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 n cities 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 n 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

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.
Sample test(s)
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


題意 理解:有n個城市,他們之間有n條單向路,如果要將a到b之間的路調轉方向,需要花c塊錢,問,在保證所有的城市都能到達另一個城市的情況下,花費最少。

解題思路:這是一個單向環問題,最後就是要求順時針走和逆時針修路最後誰花的錢最少。在存數據的時候用二維數組將題目給的本來的方向的路徑值設爲0,逆向的數值設爲c,分兩條路求花費,最後輸出最小值。


code:

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
using namespace std;
int a[105][105];
const int INF=1000;
int main()
{
    int n,a1,b,c,l,lp,r,rp;
    int mx1=0;
    int mx2=0;
    int mx;
    scanf("%d",&n);
    for(int i=1;i<=n;i++){
        for(int j=1;j<=n;j++){
            a[i][j]=INF;
        }
        a[i][i]=0;
    }
    for(int i=0;i<n;i++){
        scanf("%d%d%d",&a1,&b,&c);
        a[a1][b]=0;
        a[b][a1]=c;
    }
    if(n>3){
        for(int i=2;i<=n;i++)
            if(a[1][i]!=INF){
                l=i;
                break;
            }
        mx1+=a[1][l];
        lp=1;
        for(int i=l+1;i<=n;i++)
            if(a[1][i]!=INF){
                  r=i;
                  break;
            }
        mx2+=a[1][r];
        rp=1;
        for(int i=1;i<=n;i++){
            if(a[l][i]!=INF&&i!=lp&&i!=l){
                mx1+=a[l][i];
                lp=l;
                l=i;
                if(i==1)
                    break;
                else{
                   i=0;
                continue;
                }
            }
        }
        for(int i=1;i<=n;i++){
            if(a[r][i]!=INF&&i!=rp&&i!=r){
                mx2+=a[r][i];
                rp=r;
                r=i;
                if(i==1)
                    break;
                else{
                   i=0;
                continue;
                }
            }
        }
    }
    else
    {
        mx1=a[1][2]+a[2][3]+a[3][1];
        mx2=a[1][3]+a[3][2]+a[2][1];
    }
    mx=min(mx1,mx2);
    printf("%d\n",mx);
    return 0;
}


發佈了68 篇原創文章 · 獲贊 9 · 訪問量 4萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章