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]; 这个 ...
随机推荐
- 微信小程序wx:for循环
最近做微信小程序碰到了一些问题,和wx:for循环相关,wx:for有很多用途,例如可以用于swiper中图片的循环,也就是所谓的轮播图,也可以用于其它的循环,可以大大地减少代码量. 但wx:for. ...
- in和not in
当子查询返回的列的值是多个值,那么就不能使用比较运算符(> < = !=),使用关键字in 语法: select …..from …..where 表达式 in (子查询) 常用in替换等 ...
- C3P0数据库连接池的java实现
1.配置准备 导入jar包 c3p0-0.9.2-pre1.jar mchange-commons-0.2.jar 数据库驱动包,如:mysql-connector-java-5.1.28-bin.j ...
- nginx+nodejs+mysql+memcached服务器后台架设centos6.5
需要的下面四个工具最好都采用yum安装,不要采用编译安装的方法,因为编译安装会导致某些依赖关系丢失. nginx 作为HTTP和反向代理,处理静态页面,动态服务交由nodejs服务. nodejs作为 ...
- 流畅的python和cookbook学习笔记(八)
1.函数的默认参数必须不可变 如果函数的默认参数为可变的对象,那么默认参数在函数外被修改也会影响到函数本身的. >>> def spam(a, b=None): # b要为不可变参数 ...
- maven仓库中的LastUpdated文件删除脚本
cleanLastUpdated.bat(windows版本) @echo off rem create by NettQun rem 这里写你的仓库路径 set REPOSITORY_PATH=D: ...
- Mybaits插入记录返回主键值
某些情况进行insert时不知道主键值(主键为自增),例如系统新增用户时,有用户序号(主键 自增),用户名,密码.插入时只需插入用户名和密码,之后取得mysql自增的序号. 如下为mysql的usr表 ...
- [ERROR] Failed to execute goal org.apache.maven.plugins:maven-dependency-plugin:2.8:unpack (unpack) on project sq-integral-web: Unable to find artifact.
1.问题描述 项目maven打包报上述错误, 但是小伙伴运行好使. 2.问题解决 是idea工程编码(gbk)和项目编码(utf-8)不一致 idea->file->Other Setti ...
- 配合sublime使用flexible.js实现微信开发页面自适应
什么是flexible.js 是一个终端设备适配的解决方案.也就是说它可以让你在不同的终端设备中实现页面适配. 是一个用来适配移动端的javascript框架.根据宽度的不同设置不同的字体大小,样式间 ...
- Java BeanUtils 组件 使用
1. BeanUtils组件 1.1 简介 程序中对javabean的操作很频繁, 所以apache提供了一套开源的api,方便对javabean的操作!即BeanUtils组件. BeanUtils ...