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
", 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
", ans1);
for(int i = tp; i >= 2; i--){
int u = dot[i], v = dot[i - 1];
// printf("u = %d v = %d
", 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
", ans2);
printf("Case %d: %d
", ++kase, min(ans1, ans2));
}
return 0;
}