BZOJ1922 大陆争霸

目录

BZOJ1922 大陆争霸

题目传送门

题解

有点意思的一道题目,相当于是带限制的最短路,我们记\(d[i]\)表示第\(i\)个点被几个防御装置保护,\(d1[i]\)表示如果没有限制的话,到达第\(i\)个点的时间,\(d2[i]\)表示实际到达第\(i\)个点的时间。我们在跑\(Dijstra\)的时候,队列中储存的只能是没有被防御装置保护的点。每次都更新到达节点的\(d1[i]\)的值,然后将这个点所连的所有没有被防御装置的保护的点丢进队列中。并且更新被这个点保护的所有城市的\(d[i]\),如果\(d[i]\)为0,那么就更新这个对应节点的\(d2[i]\)的值,并把它丢到队列中。最后答案就是\(max(d1[n],d2[n])\)

code

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
bool Finish_read;
template<class T>inline void read(T &x){Finish_read=0;x=0;int f=1;char ch=getchar();while(!isdigit(ch)){if(ch=='-')f=-1;if(ch==EOF)return;ch=getchar();}while(isdigit(ch))x=x*10+ch-'0',ch=getchar();x*=f;Finish_read=1;}
template<class T>inline void print(T x){if(x/10!=0)print(x/10);putchar(x%10+'0');}
template<class T>inline void writeln(T x){if(x<0)putchar('-');x=abs(x);print(x);putchar('\n');}
template<class T>inline void write(T x){if(x<0)putchar('-');x=abs(x);print(x);}
/*================Header Template==============*/
#define PAUSE printf("Press Enter key to continue..."); fgetc(stdin);
const int M=1e5+500;
const int N=3005;
typedef pair<int,int>P;
#define fi first
#define se second
int n,m,tot;
struct edge {
    int to,nxt,w;
}E[M<<2];
int a[N][N];
int len[N],head[N];
bool vis[N];
int d[N],d1[N],d2[N];
/*==================Define Area================*/
void addedge(int u,int v,int w) {
    E[++tot].to=v;E[tot].nxt=head[u];head[u]=tot;E[tot].w=w;
}

void Dj() {
    memset(d1,0x3f,sizeof d1);
    priority_queue<P,vector<P>,greater<P> >Q;
    Q.push(P(0,1));
    d1[1]=0;
    while(!Q.empty()) {
        P p=Q.top();Q.pop();
        int o=p.se;
        if(vis[o]) continue;
        vis[o]=1;
        int mx=max(d1[o],d2[o]);
        for(int i=head[o];~i;i=E[i].nxt) {
            int to=E[i].to;
            if(d1[to]>mx+E[i].w) {
                d1[to]=mx+E[i].w;
                int tmp=max(d1[to],d2[to]);
                if(!d[to]) Q.push(P(tmp,to));
            }
        }
        for(int i=1;i<=len[o];i++) {
            int t=a[o][i];
            d[t]--;d2[t]=max(d2[t],mx);
            int tmp=max(d1[t],d2[t]);
            if(!d[t]) Q.push(P(tmp,t)); 
        }
    }
    printf("%d\n",max(d1[n],d2[n]));
}

int main() {
    memset(head,-1,sizeof head);
    read(n);read(m);
    for(int i=1,u,v,w;i<=m;i++) {
        read(u);read(v);read(w);
        addedge(u,v,w);
    }
    for(int i=1;i<=n;i++) {
        read(d[i]);
        for(int j=1,u;j<=d[i];j++) {
            read(u);
            a[u][++len[u]]=i;
        }
    }
    Dj();
    return 0;
}

猜你喜欢

转载自www.cnblogs.com/Apocrypha/p/9432894.html
今日推荐