"Shortest circuit" acdream 1198-transformers ' Mission

Source: Internet
Author: User

Problem Description

A Group of Transformers whose leader is Optimus Prime (Optimus) were assigned a mission:to destroy all DEcepticon ' s (PA day Tiger) Bases.

The bases is connected by roads. They must visit each base and place a bomb there. they start their mission at a particular base and from there they disseminate to reach each base.

The Transformers must use the available roads to travel between bases. Any of them can visit one base after another, but they must all gather at a common base when their task in done Becaus E Optimus Prime (Optimus) doesn ' t allow he teammate to run into any trouble.

Your job is to determine the minimum time needed to complete the mission.

Assume that the time required to place a bomb is negligible.

Each transformer can carry unlimited number of bombs and there are an unlimited supply of transformers for this mission.

Input

Input starts with an integer T (t≤50), denoting the number of test cases.

The first line of all case starts with a positive integer n (1≤n≤1000), where n denotes the number o F Decepticon ' s (Decepticons) bases.

The next line contains a positive an integer m (0≤m≤100000), where m is the number of roads connecting T WO bases.

Each of the next m lines contain the distinct numbers u, V, W (0≤u, V, w < n, u! = V), this means th Ere is a road from the base u to base v which costs W units of time. The bases is numbered from 0 to n-1.

The last line of all case contains the integers s, E (0≤s, e < n).

where s denotes the base from where the mission starts and e denotes the base where they must meet.

You are assume that bases would be directly connected by at the most one road.

The input would be given such that, it'll be possible to go from any base to another by using one or more roads.

Outputfor, output one line containing ' case #x: ' followed by the minimum time. Sample Input
25 60 1 21 2 23 1 24 0 33 2 33 4 14 35 51 0 11 2 31 3 34 2 23 4 14 2
Sample Output
Case #1:7Case #2:9

"Test instructions" simply means sending several people to fry N places, asking at least how long it will take.
"Analysis" to take a point to find the starting point and the focus of the shortest, the sum is to fry this place to spend the least time, and then take n places to spend the maximum value of time;
"Skill" two times BFS to find the shortest way;
Code
1#include <iostream>2#include <cstdio>3#include <cstdlib>4#include <vector>5#include <cstring>6#include <queue>7 using namespacestd;8 Const intMAXN =1010;9vector<int>G[MAXN];Ten intMAP[MAXN][MAXN], D1[MAXN], D2[MAXN], VIS[MAXN]; One intm, N; A voidBFS (intSint*d) - { -memset (D,-1,sizeof(d)); thememset (Vis,0,sizeof(Vis)); -  -     intU =s; -D[u] =0; Vis[u] =1; +queue<int>Q; - q.push (u); +      while(!q.empty ()) A     { at  -         intv =Q.front (); Q.pop (); -         //cout << "V:" << v << Endl; -         intSZ =g[v].size (); -          for(inti =0; I < sz; i++) -         { in             intW =G[v][i]; -             //cout << "W:" << w << Endl; to             if(!Vis[w]) +             { -VIS[W] =1; theD[W] = d[v]+Map[v][w]; * Q.push (w); $             }Panax Notoginseng             Else -             { the                 if(D[v]+map[v][w] <D[w]) +                 { AD[W] = d[v]+Map[v][w]; the Q.push (w); +                 } -             } $         } $  -     } - } the  - intMain ()Wuyi { the     intT scanf"%d", &T); -      for(intKase =1; Kase <= T; kase++) Wu     { -memset (Map,0,sizeof(MAP)); About          for(inti =0; I < n; i++) g[i].clear (); $scanf"%d%d", &n, &m); -          while(m--) -         { -             intu, V, t; Ascanf"%d%d%d", &u, &v, &t); +MAP[U][V] = Map[v][u] =T; the G[u].push_back (v); G[v].push_back (u); -         } $         intSt, en; scanf"%d%d", &st, &en); the BFS (St, D1); the BFS (en, D2); the         intAns =0; the          for(inti =0; I < n; i++) -ans = max (ans, d1[i]+d2[i]); inprintf"Case #%d:%d\n", Kase, ans); the //for (int i = 0; i < n; i++) printf ("%d", D2[i]); the //printf ("\ n"); About     } the     return 0; the}
View Code

Shortest path Acdream 1198-transformers ' Mission

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.