【最短路】ACdream 1198 - Transformers' Mission

时间:2023-01-12 00:16:12

Problem Description

A group of transformers whose leader is Optimus Prime(擎天柱) were assigned a mission: to destroy all Decepticon's(霸天虎) bases.

The bases are 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 because Optimus Prime(擎天柱) doesn't allow his teammate to run into any trouble.

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

You may assume that the time required to place a bomb is negligible.

Each transformer can carry unlimited number of bombs and there is 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 each case starts with a positive integer n(1 ≤ n ≤ 1000), where n denotes the number of Decepticon's(霸天虎) bases.

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

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

The last line of each case contains two 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 may assume that two bases will be directly connected by at most one road.

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

Output

For each case, output one line containing "Case #x: " followed by the minimum time.

Sample Input

2
5 6
0 1 2
1 2 2
3 1 2
4 0 3
3 2 3
3 4 1
4 3
5 5
1 0 1
1 2 3
1 3 3
4 2 2
3 4 1
4 2

Sample Output

Case #1: 7
Case #2: 9 【题意】简单说就是派好几个人去炸n个地方,问至少需要花多长时间。
【分析】任取一点分别求到起点和重点的最短路,所得之和就是炸这个地方时所需要花费的最少时间,然后取n个地方花费时间的最大值即可;
【技巧】两次BFS求最短路;
【代码】
 #include<iostream>
#include<cstdio>
#include<cstdlib>
#include<vector>
#include<cstring>
#include<queue>
using namespace std;
const int maxn = ;
vector<int> G[maxn];
int Map[maxn][maxn], d1[maxn], d2[maxn], vis[maxn];
int m, n;
void BFS(int s, int* d)
{
memset(d, -, sizeof(d));
memset(vis, , sizeof(vis)); int u = s;
d[u] = ; vis[u] = ;
queue<int> q;
q.push(u);
while(!q.empty())
{ int v = q.front(); q.pop();
//cout << "v: " << v << endl;
int sz = G[v].size();
for(int i = ; i < sz; i++)
{
int w = G[v][i];
//cout << "w: " << w << endl;
if(!vis[w])
{
vis[w] = ;
d[w] = d[v]+Map[v][w];
q.push(w);
}
else
{
if(d[v]+Map[v][w] < d[w])
{
d[w] = d[v]+Map[v][w];
q.push(w);
}
}
} }
} int main()
{
int T; scanf("%d", &T);
for(int kase = ; kase <= T; kase++)
{
memset(Map, , sizeof(Map));
for(int i = ; i < n; i++) G[i].clear();
scanf("%d%d", &n, &m);
while(m--)
{
int u, v, t;
scanf("%d%d%d", &u, &v, &t);
Map[u][v] = Map[v][u] = t;
G[u].push_back(v); G[v].push_back(u);
}
int st, en; scanf("%d%d", &st, &en);
BFS(st, d1);
BFS(en, d2);
int ans = ;
for(int i = ; i < n; i++)
ans = max(ans, d1[i]+d2[i]);
printf("Case #%d: %d\n", kase, ans);
// for(int i = 0; i < n; i++) printf("%d ", d2[i]);
// printf("\n");
}
return ;
}

【最短路】ACdream 1198 - Transformers' Mission的更多相关文章

  1. ACDream-C - Transformers&&num;39&semi; Mission(Dijastra最短路径)

    dijstra求最短路径:经典应用题目: 题意:给你一个带权值无向图,权值是A点到B点的时间,然后告诉你起点,一个人可以去炸掉一个结点或多个节点,也可以派多个人,最终这些人在终点集合,问最后一个到达终 ...

  2. 【POJ】2449 Remmarguts&&num;39&semi; Date(k短路)

    http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...

  3. poj 2449 Remmarguts&&num;39&semi; Date K短路&plus;A&ast;

    题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...

  4. poj 2449 Remmarguts&&num;39&semi; Date(第K短路问题 Dijkstra&plus;A&ast;)

    http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Subm ...

  5. 图论&lpar;A&ast;算法,K短路&rpar; :POJ 2449 Remmarguts&&num;39&semi; Date

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 25216   Accepted: 6882 ...

  6. poj 2449 Remmarguts&&num;39&semi; Date 第k短路 (最短路变形)

    Remmarguts' Date Time Limit: 4000MS   Memory Limit: 65536K Total Submissions: 33606   Accepted: 9116 ...

  7. &lpar;最短路 Floyd&rpar; P2910 &lbrack;USACO08OPEN&rsqb;寻宝之路Clear And Present Danger 洛谷

    题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...

  8. Remmarguts&&num;39&semi; Date POJ - 2449 (A&ast;搜索&vert;k短路)

    "Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...

  9. POJ 2449 - Remmarguts&&num;39&semi; Date - &lbrack;第k短路模板题&rsqb;&lbrack;优先队列BFS&rsqb;

    题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...

随机推荐

  1. thinkphp一句话疑难解决笔记 3

    错误调试, E($msg)? 这个是tp内置的E 方法, E 函数. 它是tp抛异常 的另外一种方式. 默认的异常处理方式是, 在 框架下的 ThinkPHP/Tpl/think_exception. ...

  2. 解决浏览器Adobe Flash Player不是最新版本问题

    关键:选择谷歌浏览器的PPAPI版本的flash下载直接安装即可 搜索: Adobe Flash Player PPAPI 下载地址: http://www.wmzhe.com/soft-30259. ...

  3. DES算法

    好久没写过博客啦,最近在gao搞Qt,做出漂亮的UI确实挺难的,做美工也不简单啊,好啦,言归正传,下面是实现DES的python源码,是借鉴了开源中国一个大师的源码写出来的,直接贴啦. 加密部分: # ...

  4. CentOS7:Puppet推送Zabbix Agent

    创建zabbix模块目录: $ mkdir -p /etc/puppet/modules/zabbix/{manifests,templates} 创建init.pp清单: $ cat /etc/pu ...

  5. SQL脚本循环修改数据库字段类型

    数据库在设计的时候也许考虑不全面,导致某些字段类型不太准确.比如设计的时候是varchar(1024),但是实际使用的时候却发现太小了,装不下,于是需要修改字段类型为ntext什么的. 我最近就遇到了 ...

  6. 通过IP获得IP所在地的三个接口

    http://ip.qq.com/cgi-bin/searchip?searchip1=180.168.144.211 http://ip.taobao.com/service/getIpInfo.p ...

  7. 【原创】关于Adapter的The content of the adapter has changed问题分析

    关于Adapter的The content of the adapter has changed问题分析   1.问题描述 07-28 17:22:02.162: E/AndroidRuntime(1 ...

  8. Objective-c 深浅复制

    深浅复制的定义: 浅复制:在复制时,对于被复制对象的每一层都是指针复制. 深复制:在复制时,对于被复制的对象至少有一层是对象复制. 完全复制:在复制时,对于被复制对象的每一层都是完全复制. retai ...

  9. Testlink插件工具

    目的: 使用Testlink时间长了,会发现有些功能体验不是很好,比如用例编写就无法快速复制,且展示能力很弱 使用对象: 测试人员.测试leader,技术经理 xmind2testlink:xmind ...

  10. Vue2&period;0 --- vue-cli脚手架中全局引入JQ

    第一步:安装jQuery npm/cmpn方式安装(默认安装1.7.X版本的JQ) npm/cnpm install jQuery 如果想安装更高版本的JQ那么可以选择在package.json文件下 ...