LightOJ--1049--One Way Roads(dfs)(好题)

One Way Roads
Time Limit: 500MS Memory Limit: 32768KB 64bit IO Format: %lld & %llu

Submit Status

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

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). 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

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

Source

Problem Setter: Jane Alam Jan
题意:现在有n个城市,这些城市之间有n条道路,但是这些都是有向的,我们可以将其中的一部分道路反向,使得这几个城市可以互相到达,也就是成为一个环,求最小花费
思路:因为每条道路有方向,所以这并不是一个简单的生成树,我们最后要得到的一定是一个城市之间可以互相访问的方案,这很明显应该是一个环,这个环可以是顺时针或者逆时针,但是在走的过程中可能存在一些道路没有修建,所以我们要在原来道路的基础上反向修建,在顺时针还有逆时针之间取最优

#include<cstdio>
#include<cstring>
#include<vector>
#include<algorithm>
using namespace std;
vector<int>G[200];
int n,top,map[200][200],s[200];
bool vis[200];
void dfs(int u,int sum)
{
	vis[u]=true;
	if(sum==n)
	return ;
	for(int i=0;i<G[u].size();i++)
	{
		int v=G[u][i];
		if(vis[v]) continue;
		s[top++]=v;
		dfs(v,sum+1);
	}
}
int main()
{
	int t,k=1;
	scanf("%d",&t);
	while(t--)
	{
		scanf("%d",&n);
		for(int i=0;i<=n;i++)
		G[i].clear();
		int u,v,val,ans1,ans2;
		memset(s,0,sizeof(s));
		memset(vis,0,sizeof(vis));
		memset(map,0,sizeof(map));
		for(int i=0;i<n;i++)
		{
			scanf("%d%d%d",&u,&v,&val);
			G[u].push_back(v);
			G[v].push_back(u);
			map[u][v]=val;
		}
		ans1=ans2=top=0;
		s[top++]=1;
		dfs(1,1);//访问的过程中得到一条迹,从而找到每一条路 
		for(int i=1;i<top;i++)
		{
			u=s[i-1];
			v=s[i];
			if(map[u][v]==0)//如果这条路没有修建,反向 
			ans1+=map[v][u];
		}
		if(map[s[top-1]][1]==0) 
		ans1+=map[1][s[top-1]];
		for(int i=top-1;i>=1;i--)//两个方向都要判断 
		{
			u=s[i];
			v=s[i-1];
			if(map[u][v]==0)
			ans2+=map[v][u];
		}
		if(map[1][s[top-1]]==0) 
		ans2+=map[s[top-1]][1];
		printf("Case %d: %d\n",k++,min(ans1,ans2));
	}
	return 0;
}


你可能感兴趣的:(LightOJ--1049--One Way Roads(dfs)(好题))