Cow Relays 【优先队列优化的BFS】USACO 2001 Open
Cow Relays
- Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
- Total Submission(s): 80 Accepted Submission(s): 13
Farmer John has formed a relay team for a race by choosing K (2 ≤ K ≤ 40) of his cows. The race is run on FJ's farm which has N (4 ≤ N < 800) fields numbered 1..N and M (1 ≤ M ≤ 4000) unique bidirectional pathways that connect pairs of different fields. You will be given the time it takes a cow to traverse each pathway.
The first cow begins the race in field #1 and runs to the finish line in field #N. As soon as the first cow finishes, the next cow then starts from field #1 and runs to field #N and so on. For this race, no two cows can follow precisely the same route (a route is a sequence of fields).
Write a program which computes the minimum possible time required for FJ's relay team. It is guaranteed that some minimum possible time exists. Any cows can revisit a path in her trip to the other barn if that turns out to be required for a "best" solution. As soon as a cow enters field #N, her relay leg is finished.
* Line 1: One line with three integers: K, N, and M
* Lines 2..M+1: Each line contains three integers describing a path: the starting field, the ending field, and the integer time to traverse the path (in the range 1..9500).
One line with a single integer that is the minimum possible time to run a relay.
4 5 8
1 2 1
1 3 2
1 4 2
2 3 2
2 5 3
3 4 3
3 5 4
4 5 6
23
Namely: Cow 1: 1->2->5 4
Cow 2: 1->3->5 6
Cow 3: 1->2->1->2->5 6
Cow 4: 1->2->3->5 7
题意概括:
给出一个具有 N 个顶点 M 条边的无向图,求从起点 1 到 终点 N 的K条最短路径长度之和;
解题思路:
BFS求最短路,但是顶点可以重复入队,但只允许入队 K 次,因为入队几次就说明了是求到了当前第K的最短路,我们只需要前K个,所以后面的不再入队。
优先队列能保证求到的K条路径一定是最优的。
注意:标记入队次数是在处理该节点时才标记,而不是该节点入队时标记,因为入队不一定被处理,只有被处理了才算用到了该节点来求最短路径。
AC code:
#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <vector>
#define INF 0x3f3f3f3f
#define LL long long
using namespace std;
int N, M, K, ans;
const int MAXN = ;
const int MAXM = ;
int in_c[MAXN]; struct Edge
{
int v, nxt, w;
}edge[MAXM];
int head[MAXN], cnt; struct data
{
int x, len;
bool operator<(const data &a) const {
return len > a.len; ///最小值优先
}
};
priority_queue<struct data>que; void init(){
memset(head, -, sizeof(head));
memset(in_c, , sizeof(in_c));
cnt = ;
ans = ;
} void add(int from, int to, int cost)
{
edge[cnt].v = to;
edge[cnt].w = cost;
edge[cnt].nxt = head[from];
head[from] = cnt++;
} void BFS()
{
//while(!que.empty()) que.pop();
struct data it, temp;
it.x = ;
it.len = ;
//in_c[1]++;
que.push(it);
int sum = ; while(!que.empty()){
it = que.top();que.pop();
if(it.x == N){
sum++;
ans+=it.len;
if(sum == K) return;
continue;
}
if(++in_c[it.x] > K) continue; for(int i = head[it.x]; i != -; i = edge[i].nxt){
int to = edge[i].v;
//printf("to:%d\n", to);
//if(in_c[to] < K){
//in_c[to]++;
temp.x = to;
temp.len = it.len+edge[i].w;
que.push(temp);
//}
}
} } int main()
{
int u, v, w;
//while(~scanf("%d %d %d", &K, &N, &M)){
scanf("%d %d %d", &K, &N, &M);
init();
for(int i = ; i <= M; i++){
scanf("%d %d %d", &u, &v, &w);
add(u, v, w);
add(v, u, w);
} BFS(); printf("%d\n", ans);
//} return ;
}
Cow Relays 【优先队列优化的BFS】USACO 2001 Open的更多相关文章
- poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7825 Accepted: 3068 Descri ...
- POJ3613 Cow Relays [矩阵乘法 floyd类似]
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 7335 Accepted: 2878 Descri ...
- poj3613 Cow Relays【好题】【最短路】【快速幂】
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions:9207 Accepted: 3604 Descrip ...
- HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...
- dijkstra算法之优先队列优化
github地址:https://github.com/muzhailong/dijkstra-PriorityQueue 1.题目 分析与解题思路 dijkstra算法是典型的用来解决单源最短路径的 ...
- Poj 3613 Cow Relays (图论)
Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...
- 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛
传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...
- poj 3613 Cow Relays
Cow Relays Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5411 Accepted: 2153 Descri ...
- 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)
再开始前我们先普及一下简单的图论知识 图的保存: 1.邻接矩阵. G[maxn][maxn]; 2.邻接表 邻接表我们有两种方式 (1)vector< Node > G[maxn]; 这个 ...
随机推荐
- C# dynamic json
对应普通对象,写个扩展方法,ToJson蛮方便. 但是 dynamic 类型就不行了,因为是运行时解析,只能转换为强类型 IDictionary<string, object> 才可以. ...
- [转]Oracle 初始化参数之cursor_sharing
本文转自:http://www.cnblogs.com/Richardzhu/archive/2013/01/21/2869837.html 一.Cursor_sharing简介: 这个参数是用来告诉 ...
- mybatis学习之动态sql
mybatis的动态sql语句很强大,在mapper映射文件中使用简单的标签即可实现该效果,下面一个个记录: 1.select查询 简单的select类似如下: <select id=" ...
- 使用OpenSSL(Windows x64版)将pem格式证书转换为p12格式
今天同事遇到一个问题,他获得的证书只有pem格式,而服务器要求提交p12格式,一时搞不定,来找我帮忙. 我之前也从未接触过证书类型的转换,所以上网大致搜索了一下,又亲自动手试了试,现将有关心得经验记录 ...
- 三:SSM框架整合思路
一:jar包 1.spring(包括springmvc) 2.mybatis 3.mybatis-spring整合包 4.数据库驱动 5.第三方连接池 6.json依赖包jackson 二:整合思路 ...
- 合理使用线程池 ThreadPool.QueueUserWorkItem()
//==>自建线程 new Thread(() => { //线程任务 Console.WriteLine(Thread.CurrentThread.ManagedThreadId); } ...
- OpenStack系列
一.概述 云计算介绍 OpenStack各组件详解和通信流程 二.keystone系列 三.glance系列 四.nova系列 虚拟化介绍 kvm介绍 五.neutron系列 六.horizon系列 ...
- 我的gulp第一个程序
以前都是单枪匹马的干,从没用过模块化的打包工具,加入新的团队后,模块化开发学到不少,尤其是工具的使用.团队目前较多的使用gulp,也是最流行的一款前端打包工具.最近Team开始尝试用gulp,我也只是 ...
- 移动端 line-height 不垂直居中问题
本文是从简书复制的, markdown语法可能有些出入, 想看"正版"和更多内容请关注 简书: 小贤笔记 一般情况下, 我们把 line-height 的值设置为 height 的 ...
- easyui window窗口 不垂直居中
$('#hotelDetailWrap').window({ width: 1040, height: 160, collapsible: false, minimizable: false, max ...