One-Roads (search)

Source: Internet
Author: User

One-Roads Time Limit:500MS Memory Limit:32768KB 64bit IO Format:%LLD &%llusubmit Status Practice Lightoj 1049

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 is connect Ed directly to exactly-cities, and from each of the city it is possible to the 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 ge T from some of the cities to some others. Now for each road are known in which direction the traffic are directed at it, and the cost of redirecting the traffic. What's the smallest amount of money the government should spend on the redirecting of roads so then from every city you c An 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 was described by three integers ai, Bi, ci (1ai,bi< /c10>N,ai Bi, 1 ≤ ci (+)-road is directed from City Ai to city Bi, redirecting the traffic costs CI.

Output

For each case of input, you had to print the case number and the smallest amount of money the government should spend on t He redirecting of roads so, from every, 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

Puzzle: One-way map, ask to join the side, to find the minimum cost;

Guaranteed to connect two points per point;

Code:

#include <iostream>#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <String>#include <map>#include <vector>#include <Set>UsingNamespaceStdConstint INF =0x3f3f3f3f;#define MEM (x, y) memset (x, y, sizeof (x))typedefLongLongLL;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];IntAnsIntNint dot[110];IntTpvoid Dfs (Intu) {Forint i =0; I < vec[u].size (); i++){int v =Vec[u][i];if (Vis[v])Continue; DOT[++TP] =V VIS[V] =1; DFS (v); }}IntMain () {int T, A, B, c, Kase =0; scanf"%d", &T);while (t--) {scanf ("%d", &n); MEM (MP,0); Mem (Vis,0);Forint i =0; I <110; i++) Vec[i].clear ();Forint 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; Dfs1);int ans1 =0, Ans2 =0;Forint i =2; I <= tp; i++){int u = dot[i-1], V =Dot[i];//printf ("U =%d v =%d\n", 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\n", ans1);Forint i = TP; I >=2; i--){int u = dot[i], v = dot[i-1];//printf ("U =%d v =%d\n", 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\n", ans2); printf ("Case%d:%d\n", + +Kase, min (ans1, ans2));} return 0;}                 

One-Roads (search)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.