BZOJ 1922 大陆争霸

Posted on 2017-10-07 21:04  ziliuziliu  阅读(129)  评论(0编辑  收藏  举报

有点迷,spfa好像不好写,必须dijkstra

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
#define maxv 3050
#define maxe 70050
using namespace std;
int n,m,x,y,z,l,cnt[maxv],con[maxv][maxv],d1[maxv],d2[maxv],g[maxv],nume=1;
struct pnt
{
    int v,val;
    pnt (int v,int val):v(v),val(val) {}
    pnt () {}
    friend bool operator < (const pnt &x,const pnt &y)
    {
        return x.val>y.val;
    }
}p[maxv];
priority_queue <pnt> q;
bool vis[maxv];
struct edge
{
    int v,w,nxt;
}e[maxe];
void addedge(int u,int v,int w)
{
    e[++nume].v=v;e[nume].w=w;
    e[nume].nxt=g[u];g[u]=nume;
}
void dijkstra()
{
    q.push(pnt(1,0));
    for (int i=2;i<=n;i++) d1[i]=1000000000;
    while (!q.empty())
    {
        pnt head=q.top();q.pop();
        if (vis[head.v]) continue;vis[head.v]=true;
        for (int i=g[head.v];i;i=e[i].nxt)
        {
            int v=e[i].v;
            if (d1[v]>max(d1[head.v],d2[head.v])+e[i].w)
            {
                d1[v]=max(d1[head.v],d2[head.v])+e[i].w;
                if (!cnt[v]) q.push(pnt(v,max(d1[v],d2[v])));
            }
        }
        for (int i=1;i<=con[head.v][0];i++)
        {
            int v=con[head.v][i];
            cnt[v]--;d2[v]=max(d2[v],max(d1[head.v],d2[head.v]));
            if (!cnt[v]) q.push(pnt(v,max(d1[v],d2[v])));
        }
    }
}
int main()
{
    scanf("%d%d",&n,&m);
    for (int i=1;i<=m;i++)
    {
        scanf("%d%d%d",&x,&y,&z);
        if (x!=y) addedge(x,y,z);
    }
    for (int i=1;i<=n;i++)
    {
        scanf("%d",&cnt[i]);
        for (int j=1;j<=cnt[i];j++)
        {
            scanf("%d",&x);
            con[x][++con[x][0]]=i;
        }
    }
    dijkstra();
    printf("%d\n",max(d1[n],d2[n]));
    return 0;

}