1. 程式人生 > >BZOJ2662: [BeiJing wc2012]凍結(洛谷P4822)

BZOJ2662: [BeiJing wc2012]凍結(洛谷P4822)

分層圖最短路

這道題,稍微改一改就好了。

程式碼:

#include<queue>
#include<cctype>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define N 5005
#define M 500005
#define F inline
using namespace std;
struct edge{ int nxt,to,d; }ed[M<<1];
struct P{ int x,d; };
int n,m,k,p,s,t,ans=1e9,
h[N],d[N]; priority_queue <P> q; F char readc(){ static char buf[100000],*l=buf,*r=buf; if (l==r) r=(l=buf)+fread(buf,1,100000,stdin); return l==r?EOF:*l++; } F int _read(){ int x=0; char ch=readc(); while (!isdigit(ch)) ch=readc(); while (isdigit(ch)) x=(x<<3)+(x<<1)+(ch^48),ch=readc()
; return x; } #define add(x,y,z) ed[++k]=(edge){h[x],y,z},h[x]=k F bool operator < (P a,P b){ return a.d>b.d; } F int Dij(){ for (int i=1;i<=n*(p+1);i++) d[i]=1e9; for (q.push((P){s,d[s]=0});!q.empty();q.pop()){ if (q.top().d>d[q.top().x]) continue; int x=q.top().x; for (int i=h[
x],v;i;i=ed[i].nxt) if (d[x]+ed[i].d<d[v=ed[i].to]) d[v]=d[x]+ed[i].d,q.push((P){v,d[v]}); } for (int i=0;i<=p;i++) ans=min(ans,d[t+n*i]); return ans; } int main(){ n=_read(),m=_read(),p=_read(),s=1,t=n; for (int i=1,x,y,z;i<=m;i++){ x=_read(),y=_read(),z=_read(),add(x,y,z),add(y,x,z); for (int j=0,u=0,v=n;j<p;j++,u+=n,v+=n){ add(x+u,y+v,z/2),add(y+u,x+v,z/2); add(x+v,y+v,z),add(y+v,x+v,z); } } return printf("%d\n",Dij()),0; }