Cow Relays

  • Time Limit: 1000/1000 MS (Java/Others)     Memory Limit: 65536/65536 K (Java/Others)
  • Total Submission(s): 80     Accepted Submission(s): 13
Description

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.

Input

* Line 1: One line with three integers: KN, 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).

Output

One line with a single integer that is the minimum possible time to run a relay.

Sample Input

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

Sample Output

23

Hint

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的更多相关文章

  1. poj3613:Cow Relays(倍增优化+矩阵乘法floyd+快速幂)

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7825   Accepted: 3068 Descri ...

  2. POJ3613 Cow Relays [矩阵乘法 floyd类似]

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 7335   Accepted: 2878 Descri ...

  3. poj3613 Cow Relays【好题】【最短路】【快速幂】

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions:9207   Accepted: 3604 Descrip ...

  4. HDU6582 Path【优先队列优化最短路 + dinic最大流 == 最小割】

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6582 来源:2019 Multi-University Training Contest 1 题目大意 ...

  5. dijkstra算法之优先队列优化

    github地址:https://github.com/muzhailong/dijkstra-PriorityQueue 1.题目 分析与解题思路 dijkstra算法是典型的用来解决单源最短路径的 ...

  6. Poj 3613 Cow Relays (图论)

    Poj 3613 Cow Relays (图论) 题目大意 给出一个无向图,T条边,给出N,S,E,求S到E经过N条边的最短路径长度 理论上讲就是给了有n条边限制的最短路 solution 最一开始想 ...

  7. 地铁 Dijkstra(优先队列优化) 湖南省第12届省赛

    传送门:地铁 思路:拆点,最短路:拆点比较复杂,所以对边进行最短路,spfa会tle,所以改用Dijkstra(优先队列优化) 模板 /******************************** ...

  8. poj 3613 Cow Relays

    Cow Relays Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 5411   Accepted: 2153 Descri ...

  9. 最短路算法模板合集(Dijkstar,Dijkstar(优先队列优化), 多源最短路Floyd)

    再开始前我们先普及一下简单的图论知识 图的保存: 1.邻接矩阵. G[maxn][maxn]; 2.邻接表 邻接表我们有两种方式 (1)vector< Node > G[maxn]; 这个 ...

随机推荐

  1. [转]Asp.Net Web API 2第十七课——Creating an OData Endpoint in ASP.NET Web API 2(OData终结点)

    本文转自:http://www.cnblogs.com/aehyok/p/3545824.html 前言 很久没更新博客了,加上刚过年,现在准备重新开战,继续自己的学习之路.本文已同步到Web API ...

  2. requset获取post提交的请求参数

    1.请求体的内容通常是通过post来提交的,格式是 username=zhansan&password=123&hobby=football||&hobby=basketbal ...

  3. js-原始类型和声明变量

    ** Java的基本数据类型:byte.short.int.long.float.double.char.boolean ** 定义变量 都是用关键字 var(ES6中可以使用const和let来定义 ...

  4. IE8 td元素 width无效的bug;

    不经意间做项目发现IE的td在某种情况下好奇怪,自己设置的width不起作用: 后经google大法,发现解决方案:已验证过完美解决bug; <table style="width:  ...

  5. [小北De编程手记] : Lesson 04 - Selenium For C# 之 API 上

    这一部分,我准备向大家介绍Selenium WebDriver的常用API,学习这部分内容需要大家最好有一些简单的HTML相关知识,本文主要涉及到以下内容: Selenium API:元素检查 Sel ...

  6. ERP与电子商务的集成

    目前现状: 一般来说,企业中存在三种流:物资流.资金流和信息流,其中,信息流不是孤立存在的,它与物资流和资金流密切相关,反映了物资和资金流动前.流动中和流动后的状况. 电子商务与ERP被分裂开来,没有 ...

  7. hiho一下 第一周 最长回文子串

    时间限制:1000ms 单点时限:1000ms 内存限制:64MB 描述 小Hi和小Ho是一对好朋友,出生在信息化社会的他们对编程产生了莫大的兴趣,他们约定好互相帮助,在编程的学习道路上一同前进. 这 ...

  8. 10_Redis实现分布式锁

    来源:吴兆锋, https://wudashan.cn/2017/10/23/Redis-Distributed-Lock-Implement/ 前言 分布式锁一般有三种实现方式:1. 数据库乐观锁: ...

  9. Android 高速录像(1)

    package com.kirin.voltage.activity; import java.io.File;import java.io.IOException;import java.util. ...

  10. 异步nodejs代码的同步样子写法样例

    异步nodejs代码的同步样子写法样例 js的异步嵌套太深代码将不好看.尤其在用node的时候这种情况会大量出现. 这里用node连接redis,做一个用户注册的简单例子来说明.例如用redis做存储 ...