codeforces 24A Ring road

来源:互联网 发布:麦田的守望者 知乎 编辑:程序博客网 时间:2024/06/10 02:18

今天写个题解。
一个图论的水题,当然,对我来说简直一筹莫展。
codeforces 24A

A. Ring road

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行城市间已有道路情况:起点城市ai,终点城市bi,两城间距离ci.
要求输出最少需要花费多少金钱改单向道为双向道(1金钱 = 1距离)
如第一组测试数据:
城市1→3有距离1,
1→2 : 5,
3→2 : 1;
要形成环形公路,可将1→2改为双向道路,即花费金钱5,
或将其他两条路改为双向道路,即花费金钱2,
可见答案 = 2.

解:
由经过所有城市可知,涉及图的搜索;
由环形道路可知,遍历该图只需一条回路即可,应该用dfs;
由于一个城市有且仅有两条道路通向其他城市,因此只需一维数组来记录两城之间距离;
故解法为:
1.读入n行,第i行读入道路i信息:起点城市begin[i],终点城市end[i],距离/花费cost[i];
2.计算总距离sum;
3.以第一条路的终点城市end[1]为起点开始dfs;
4.道路i方向与上一条道路冲突则将其反向,记录cost[i]并累加于count;不冲突则继续,直至道路i终点为第一条路起点;
5.比较count与sum - count,输出其最小值.

代码如下:

/**Author : Flint_x *Created Time : 2015-02-28 12:03:16 *File name : g.cpp */#include<iostream>#include<sstream>#include<fstream>#include<vector>#include<list>#include<deque>#include<queue>#include<stack>#include<map>#include<set>#include<bitset>#include<algorithm>#include<cstdio>#include<cstdlib>#include<cstring>#include<cctype>#include<cmath>#include<ctime>#include<iomanip>using namespace std;const double eps(1e-8);typedef long long lint;int begin[105],end[105],cost[105];lint result = 0;int n;void dfs( int start , int self){    for( int i = 1 ; i <= n ; i++){        if(self == i) continue;        if(start == begin[i]){            if(end[i] == begin[1]) return;            dfs(end[i],i);        }        else if(start == end[i]){            result += cost[i];            if(begin[i] == begin[1]) return;//          cout << result << endl;            dfs(begin[i],i);        }    }}int main(){        cin >> n;        lint sum = 0;        for( int i = 1 ; i <= n ; i++){            cin >> begin[i] >> end[i] >> cost[i];            sum += cost[i];        }//      cout << sum << endl;        dfs(end[1],1);        if(result > sum - result) result = sum - result;        cout << result << endl;    return 0;}
0 0
原创粉丝点击