[USACO14OPEN]GPS的决斗Dueling GPS's [最短路]

[USACO14OPEN]GPS的决斗Dueling GPS's

luoguP3106 bzoj3538

一道bzoj权限题? hin水? 分别跑三次dijkstra

更难的差不多的题:SDOI2009

#include
using namespace std;
#define Max(x,y) ((x)>(y)?(x):(y))
#define Min(x,y) ((x)<(y)?(x):(y))
#define ll long long
const int N=10000+10,M=50000+10,inf=0x3f3f3f3f;
typedef pairpii;
int n,m,K,s,t,w2[M];
template void rd(t &x){
    x=0;int w=0;char ch=0;
    while(!isdigit(ch)) w|=ch=='-',ch=getchar();
    while(isdigit(ch)) x=(x<<1)+(x<<3)+(ch^48),ch=getchar();
    x=w?-x:x;
}


int head[N],tot=0;
struct edge{int v,w,nxt;}e[M];
void add(int u,int v,int w){
    e[++tot]=(edge){v,w,head[u]},head[u]=tot;
}

int dis[N],nw[M],tg[M];bool vis[N];
priority_queue,greater >q;
void dij(){
    memset(nw,0,sizeof(nw));
    memset(vis,0,sizeof(vis));
    memset(dis,inf,sizeof(dis));
    dis[s]=0,q.push(make_pair(0,s));
    while(!q.empty()){
        int u=q.top().second;q.pop();
        if(vis[u]) continue;
        vis[u]=1;
        for(int i=head[u],v,w;i;i=e[i].nxt)
        if(dis[v=e[i].v]>dis[u]+(w=e[i].w)){
            ++tg[nw[v]],nw[v]=i,--tg[i];
            q.push(make_pair(dis[v]=dis[u]+w,v));
        }
    }
} 

int main(){
    freopen("in.txt","r",stdin);
    rd(n),rd(m),s=n;
    for(int i=1,u,v,w1;i<=m;++i) rd(u),rd(v),rd(w1),rd(w2[i]),add(v,u,w1),tg[i]=2;
    dij();
    for(int i=1;i<=m;++i) e[i].w=w2[i];
    dij();
    for(int i=1;i<=m;++i) e[i].w=tg[i];
    dij();
    printf("%d",dis[1]);
    return 0;
}

你可能感兴趣的:([USACO14OPEN]GPS的决斗Dueling GPS's [最短路])