Codeforces Round 930 (Div. 1) C dij 建图

离较好的方法差一点。

考虑到了可以按照枚举属性并按照当前属性从小到大排序,这样可以从一个点到大另一个点。

设当前在排序序列中点为\(i\)\(i\)走向\(k,i>=k\)需要支付\(c_k\)的代价。

\(i\)\(k,i<k\)则需\(k-i+c_k\)的代价。

则对于不同的\(i\)由于代价没有连续性,当时想的是每个属性开个线段树求最小决策。

实际上只需将\(c_k\)赋值到点权上即可完成建图。

图边数为\(nm\)点数也为\(nm\)。故复杂度为\(nmlog(nm)\)

code
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<ctime>
#include<cctype>
#include<queue>
#include<deque>
#include<stack>
#include<iostream>
#include<iomanip>
#include<cstdio>
#include<cstring>
#include<string>
#include<ctime>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<queue>
#include<deque>
#include<stack>
#include<vector>
#include<algorithm>
#include<utility>
#include<bitset>
#include<set>
#include<map>
#define ll long long
#define db double
#define INF 1000000000
#define inf 100000000000000000ll
#define ldb long double
#define pb push_back
#define put_(x) printf("%d ",x);
#define putl_(x) printf("%lld ",x);
#define get(x) x=read()
#define putl(x) printf("%lld\n",x)
#define rep(p,n,i) for(int i=p;i<=n;i+=1)
#define fep(n,p,i) for(int i=n;i>=p;--i)
#define go(x) for(int i=lin[x],tn=ver[i];i;tn=ver[i=nex[i]])
#define pii pair<int,int>
#define mk make_pair
#define gf(x) scanf("%lf",&x)
#define pf(x) ((x)*(x))
#define uint unsigned long long
#define ui unsigned
#define sq sqrt	
#define x(w) t[w].x
#define r(w) t[w].r
#define l(w) t[w].l
#define yy p<<1|1
#define zz p<<1
#define sum(w) t[w].sum
#define mod 1000000007
#define sc(A) scanf("%d",&A)
#define scl(A) scanf("%lld",&A)	
#define scs(A) scanf("%s",A);
#define put(A) printf("%d\n",A)
#define min(x,y) (x>=y?y:x)
#define max(x,y) (x>=y?x:y)
#define sub(x,y) (x-y<0?x-y+mod:x-y)
#define F first
#define S second
using namespace std;
const int MAXN=400010<<1;
int T,len;
int n,m,cnt;
int c[MAXN],id[MAXN],b[MAXN],p[MAXN];
int lin[MAXN],ver[MAXN<<2],nex[MAXN<<2],e[MAXN<<2];
ll dis[MAXN],vis[MAXN];
vector<int>a[MAXN];
void add(int x,int y,int z)
{
    ver[++len]=y;
    nex[len]=lin[x];
    lin[x]=len;
    e[len]=z;
    //cout<<z<<endl;
}
void cle()
{
    len=0;
    rep(1,cnt,i)
    {
        lin[i]=0;
        c[i]=0;
        a[i].clear();
    }
}
int cmp(int a,int B){return b[a]>b[B];}
priority_queue<pair<int,int> >q;
void dij()
{
    rep(1,cnt,i)dis[i]=inf,vis[i]=0;
    dis[1]=0;q.push(mk(0,1));
    while(q.size())
    {
        int x=q.top().second;
        q.pop();
        if(vis[x])continue;
        vis[x]=1;
        go(x)
        {
            
            if(dis[tn]>dis[x]+e[i]+c[tn])
            {
                dis[tn]=dis[x]+e[i]+c[tn];
                q.push(mk(-dis[tn],tn));
            }
        }
    }
}
int main()
{
    //freopen("1.in","r",stdin);
    sc(T);
    while(T--)
    {
        cle();
        sc(n);sc(m);cnt=n;
        rep(1,n,i)sc(c[i]);
        rep(1,n,i)
        {
            p[i]=i;
            int x;
            rep(1,m,j)
            {
                sc(x);
                a[i].pb(x);
            }
        }
        rep(1,m,j)
        {
            rep(1,n,k)
            {
                b[k]=a[k][j-1];
                id[k]=++cnt;
                add(k,id[k],0);
                add(id[k],k,0);
            }
            sort(p+1,p+1+n,cmp);
            rep(2,n,i)
            {
                //cout<<p[i]<<' ';
                //cout<<endl;
                add(id[p[i]],id[p[i-1]],0);
                add(id[p[i-1]],id[p[i]],-a[p[i]][j-1]+a[p[i-1]][j-1]);
            }
        }
        dij();
        putl(dis[n]);
    }
    return 0;
}
posted @ 2024-03-04 17:13  chdy  阅读(13)  评论(0编辑  收藏  举报