当先锋百科网

首页 1 2 3 4 5 6 7

题目链接

比赛的时候一看,这不是最短路吗,然后敲了一个最短路。

然后比赛完发现大家基本都写的dp,我真是个憨憨,dp3行

最短路就建个简单的图,dp就是从上一维转化过来就是了

优秀的dp:

//#pragma comment (linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
//freopen("E://1.in","r",stdin);
//freopen("E://1.out","w",stdout);
typedef unsigned long long ull;
typedef long long ll;
#define pii make_pair
#define pr pair<int,int>
const int inff = 0x3f3f3f3f;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-10;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const long long inFF = 9223372036854775807;
const int mod=1e9+7;
const int maxn=2e5+5;
int a[maxn],b[maxn];
int dp[maxn][2];
int main()
{
    ios::sync_with_stdio(false);
    int n,c;
    cin>>n>>c;
    for(int i=2;i<=n;i++) cin>>a[i],dp[i][0]=inff;
    for(int i=2;i<=n;i++) cin>>b[i],dp[i][1]=inff;
    dp[1][0]=0,dp[1][1]=c;
    for(int i=2;i<=n;i++)
    {
        dp[i][0]=min(dp[i-1][0]+a[i],dp[i-1][1]+b[i]);
        dp[i][1]=min(dp[i-1][0]+a[i]+c,dp[i-1][1]+b[i]);
        dp[i][0]=min(dp[i][0],dp[i][1]);
    }
    for(int i=1;i<=n;i++) cout<<min(dp[i][0],dp[i][1])<<" ";
}

SB最短路:

//#pragma comment (linker, "/STACK:102400000,102400000")
#include<bits/stdc++.h>
#include<stdio.h>
#include<string.h>
#include<string>
#include<iostream>
#include<algorithm>
#include<math.h>
#include<set>
#include<stack>
#include<vector>
#include<map>
#include<queue>
#include<list>
#include<time.h>
#define myself i,l,r
#define lson i<<1
#define rson i<<1|1
#define Lson i<<1,l,mid
#define Rson i<<1|1,mid+1,r
#define half (l+r)/2
#define lowbit(x) x&(-x)
#define min4(a, b, c, d) min(min(a,b),min(c,d))
#define min3(x, y, z) min(min(x,y),z)
#define max3(x, y, z) max(max(x,y),z)
#define max4(a, b, c, d) max(max(a,b),max(c,d))
#define pii make_pair
#define pr pair<ll,int>
//freopen("E://1.in","r",stdin);
//freopen("E://1.out","w",stdout);
typedef unsigned long long ull;
typedef long long ll;
const int inff = 0x3f3f3f3f;
const long long inFF = 9223372036854775807;
const int dir[4][2] = {0, 1, 0, -1, 1, 0, -1, 0};
const int mdir[8][2] = {0, 1, 0, -1, 1, 0, -1, 0, 1, 1, -1, 1, 1, -1, -1, -1};
const double eps = 1e-10;
const double PI = acos(-1.0);
const double E = 2.718281828459;
using namespace std;
const int mod=1e9+7;
const int maxn=1e6+5;
int head[maxn],sign;
struct node
{
    int to,p,val;
}edge[maxn<<1];
int n,m;
ll d[maxn];
void init()
{
    for(int i=0;i<=3*n;i++) head[i]=-1;
    sign=0;
}
void add(int u,int v,int val)
{
    edge[sign]=node{v,head[u],val};
    head[u]=sign++;
}
void dij()
{
    for(int i=1;i<=3*n;i++) d[i]=inFF;
    d[0]=0;
    priority_queue<pr,vector<pr>,greater<pr> >q;
    q.push(pii(0,0));
    while(!q.empty())
    {
        pr now=q.top();
        q.pop();
        if(d[now.second]>now.first) continue;
        for(int i=head[now.second];~i;i=edge[i].p)
        {
            int v=edge[i].to;
            if(d[v]>edge[i].val+d[now.second])
            {
                d[v]=edge[i].val+d[now.second];
                q.push(pii(d[v],v));
            }
        }
    }
}
int main()
{
    cin>>n>>m;
    init();
    int x;
    for(int i=1;i<n;i++)
        scanf("%d",&x),add(i,i+1,x);
    for(int i=1;i<n;i++)
        scanf("%d",&x),add(i+n,i+1+n,x);
    for(int i=1;i<=n;i++) add(i,i+2*n,0),add(i+n,i+2*n,0),add(i,i+n,m),add(i+n,i,0);
    add(0,1,0),add(0,1+n,m);
    dij();
    for(int i=2*n+1;i<=3*n;i++) printf("%lld%c",d[i],(i==n)?'\n':' ');
}