【BZOJ3892】【Usaco2014 Dec】Marathon (Silver and Bronze) 暴力动规

广告

#include <stdio.h>
int main()
{
    puts("转载请注明出处[vmurder]谢谢");
    puts("网址:blog.csdn.net/vmurder/article/details/43970671");
}

题解

——Silver

f[i][j]表示到第i个跳过了j个的最小值

然后暴力从前转移。

它的时间复杂度是1.25亿,但是常数远远远远小于1

——Bronze

跟银组的一样,只不过改改数组大小,然后m直接赋值1就好了。

银组代码:

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 505
#define INF 0x3f3f3f3f3f3f3f3fll
using namespace std;
long long n,m;
long long f[N][N],x[N],y[N];
int main()
{
    freopen("test.in","r",stdin);
    cin>>n>>m;
    int i,j,k;
    for(i=1;i<=n;i++)cin>>x[i]>>y[i];
    memset(f,0x3f,sizeof f);
    f[1][0]=0;
    for(i=2;i<=n;i++)
    {
        for(j=0;j<i-1&&j<=m;j++)
        {
            for(k=i-1;i-k-1<=j&&k;k--)
            {
                f[i][j]=min(f[i][j],f[k][j-(i-k-1)]+abs(x[i]-x[k])+abs(y[i]-y[k]));
            }
        }
    }
    long long ans=INF;
    for(i=0;i<=m;i++)ans=min(ans,f[n][m]);
        cout<<ans<<endl;
    return 0;
}
时间: 2024-10-12 09:28:10

【BZOJ3892】【Usaco2014 Dec】Marathon (Silver and Bronze) 暴力动规的相关文章

[bzoj3892] [Usaco2014 Dec]Marathon

瞎DP一波. f[i][j]:表示已到达或跳过前i个点,总共跳了j个点,并且目前在第i个点的最小总代价. f[i][j]=min{ f[k][j-(i-k-1)] }+dis(k,i),(dis(k,i)表示两点间距离. 时间复杂度O(n^3)本来以为过不了的.. 1 #include<cstdio> 2 #include<iostream> 3 #include<cstring> 4 #include<algorithm> 5 using namespac

3892: [Usaco2014 Dec]Marathon

3892: [Usaco2014 Dec]Marathon Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 169  Solved: 100[Submit][Status][Discuss] Description Unhappy with the poor health of his cows, Farmer John enrolls them in an assortment of different physical fitness acti

BZOJ 3892 Usaco2014 Dec Marathon DP

题目大意:给出平面上的一些点,要求按顺序遍历,费用是两点之间的曼哈顿距离,可以跳过k次,问最少需要花费多少. 思路:O(n^3)dp就行了. CODE: #define _CRT_SECURE_NO_WARNINGS #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 510 using namespace std; struct

BZOJ 3892 [Usaco2014 Dec]Marathon 动态规划

题目大意:给定n个点,定义从一个点到另一个点的距离为曼哈顿距离,要求从点1依次走到点n,中途可以跳过k个点不走,求最小距离和 令f[i][j]表示从第一个点走到第i个点中途跳过j次的最小距离和 则有f[i][j]=min{f[i-k-1][j-k]+dis[i-k-1][i]} 时间复杂度O(n^3) #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #

3891: [Usaco2014 Dec]Piggy Back

3891: [Usaco2014 Dec]Piggy Back Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 116  Solved: 92[Submit][Status][Discuss] Description Bessie and her sister Elsie graze in different fields during the day, and in the evening they both want to walk back

3893: [Usaco2014 Dec]Cow Jog

3893: [Usaco2014 Dec]Cow Jog Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 174  Solved: 87[Submit][Status][Discuss] Description The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N

bzoj3891[Usaco2014 Dec]Piggy Back*

bzoj3891[Usaco2014 Dec]Piggy Back 题意: 给定一个N个点M条边的无向图,其中Bessie在1号点,Elsie在2号点,它们的目的地为N号点.Bessie每经过一条边需要消耗B点能量,Elsie每经过一条边需要消耗E点能量.当它们相遇时,它们可以一起行走,此时它们每经过一条边需要消耗P点能量.求它们两个到达N号点时最少消耗多少能量.n,m≤40000. 题解: 先求出以1.2.n为源点的最短路(因为边权为1所以用bfs).答案初始设为1到n的最短路*B+2到n的最

Bzoj3893 [Usaco2014 Dec]Cow Jog

Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 302  Solved: 157 Description The cows are out exercising their hooves again! There are N cows jogging on an infinitely-long single-lane track (1 <= N <= 100,000). Each cow starts at a distinct position

LA UVaLive 6623 Battle for Silver (最大值,暴力)

题意:给定一个图,让你找一个最大的子图,在这个子图中任何两点都有边相连,并且边不交叉,求这样子图中权值最大的是多少. 析:首先要知道的是,要想不交叉,那么最大的子图就是四个点,否则一定交叉,然后就暴力就好,数据水,不会TLE的,才100多ms 代码如下: #pragma comment(linker, "/STACK:1024000000,1024000000") #include <cstdio> #include <string> #include <