【最短路】ACdream 1198 - Transformers' Mission
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
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的更多相关文章
- ACDream-C - Transformers' Mission(Dijastra最短路径)
dijstra求最短路径:经典应用题目: 题意:给你一个带权值无向图,权值是A点到B点的时间,然后告诉你起点,一个人可以去炸掉一个结点或多个节点,也可以派多个人,最终这些人在终点集合,问最后一个到达终 ...
- 【POJ】2449 Remmarguts' Date(k短路)
http://poj.org/problem?id=2449 不会.. 百度学习.. 恩. k短路不难理解的. 结合了a_star的思想.每动一次进行一次估价,然后找最小的(此时的最短路)然后累计到k ...
- poj 2449 Remmarguts' Date K短路+A*
题目链接:http://poj.org/problem?id=2449 "Good man never makes girls wait or breaks an appointment!& ...
- poj 2449 Remmarguts' Date(第K短路问题 Dijkstra+A*)
http://poj.org/problem?id=2449 Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Subm ...
- 图论(A*算法,K短路) :POJ 2449 Remmarguts' Date
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 25216 Accepted: 6882 ...
- poj 2449 Remmarguts' Date 第k短路 (最短路变形)
Remmarguts' Date Time Limit: 4000MS Memory Limit: 65536K Total Submissions: 33606 Accepted: 9116 ...
- (最短路 Floyd) P2910 [USACO08OPEN]寻宝之路Clear And Present Danger 洛谷
题意翻译 题目描述 农夫约翰正驾驶一条小艇在牛勒比海上航行. 海上有N(1≤N≤100)个岛屿,用1到N编号.约翰从1号小岛出发,最后到达N号小岛. 一张藏宝图上说,如果他的路程上经过的小岛依次出现了 ...
- Remmarguts' Date POJ - 2449 (A*搜索|k短路)
"Good man never makes girls wait or breaks an appointment!" said the mandarin duck father. ...
- POJ 2449 - Remmarguts' Date - [第k短路模板题][优先队列BFS]
题目链接:http://poj.org/problem?id=2449 Time Limit: 4000MS Memory Limit: 65536K Description "Good m ...
随机推荐
- homework-01 最大子串和
题目描述 对于一个给定的数列,求该数列最大的子串和(连续) 问题分析 处理发生区间上的问题时,经常会用一个非常简单经典的思路——部分和(也有叫前缀和).部分和的思想在很多复杂的区间上的算法中都有应用, ...
- Spring Auto scanning components
Normally you declare all the beans or components in XML bean configuration file, so that Spring cont ...
- HDU4570----Multi-bit Trie----简单的DP
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=4570 题目意思: 给你N个数 要你分成多段,每段长度不能超过20 是的sum(ai*(2^bi))最小 ...
- OOP 6大基本原则
1.开闭原则: 对扩展开发.对修改关闭. 2.里氏替换原则:子类替换父类(可以用父类对象的任何地方都可以用子类对象代替) 3.依赖倒置原则:程序要依赖于抽象接口,不要依赖于具体实现.简单的说就是要求对 ...
- HTTP Header 简介
HTTP Header 简介 HTTP(HyperTextTransferProtocol)即超文本传输协议,目前网页传输的的通用协议.HTTP协议采用了请求/响应模型,浏览器或其他客户端发出请求,服 ...
- Unity3D之游戏暂停制作方法记录
在游戏开发中我们一般都需要涉及到一个功能:游戏暂停,但是这里指的暂停仅仅是核心模块的暂停,并不是整个游戏都暂停,比如一些UI和UI上的动画与特效是不能被暂停的,整个游戏都暂停了玩家该如何继续游戏呢. ...
- SQL中DATE和DATETIME类型不能直接作比较
如题,今天纠结了一天的问题. 在存储过程中定义了两个datetime类型的时间,然后把这个两个时间作为where条件中一个date字段between的两个时间段,结果无论如何都不执行... 就像 u ...
- python常用字符串操作
#!/usr/bin/env python name='cunzhang' print(name.capitalize())#首字母大写 print(name.count('n'))#统计字符有几个 ...
- usr/bin/ld: cannot find 错误解决方法
参考:http://blog.siyebocai.cn/20100324_5p424qs7.html 通常在软件编译时出现的usr/bin/ld: cannot find -lxxx的错误,主要的原因 ...
- ArcGIS Add-in开发(一)--获取选定要素的属性值
刚刚接触AE开发,记录一下自己的学习心得! 欢迎大家一起交流探讨! 最近做大赛,突然想到可以让项目更加直观的操作,就在项目中加了幅底图(底图很简单) 我想在arcmap中选中相应的要素后,在后台通过写 ...