其他分享
首页 > 其他分享> > luogu P3238 [HNOI2014]道路堵塞

luogu P3238 [HNOI2014]道路堵塞

作者:互联网

代码:

#include<cstdio>
#include<iostream>
#include<cstring>
#include<algorithm>
#include<deque>
#include<queue>

using namespace std;

deque <int> q;
priority_queue <pair<int,int> > Q;
const int N=1000009,INF=1<<30;
int n,m,L,dis[N],inq[N],x[N],y[N],z[N],l[N],cnt,del[N],head[N],D[N],rev[N];
struct Edge
{
	int nxt,to,w;
}g[N*2];

void add(int from,int to,int w)
{
	g[++cnt].nxt=head[from];
	g[cnt].to=to;
	g[cnt].w=w;
	head[from]=cnt;
}

void SPFA(int S)
{
	q.push_front(S);
	while(!q.empty())
	{
		int x=q.front();q.pop_front();
		inq[x]=0;
		for (int i=head[x];i;i=g[i].nxt)
		{
			int v=g[i].to;
			if(dis[v]<=dis[x]+g[i].w||del[i])
				continue;
			dis[v]=dis[x]+g[i].w;
			if(!inq[v])
			{
				inq[v]=1;
				if(!q.empty()&&dis[q.front()]>=dis[v])
					q.push_front(v);
				else
					q.push_back(v);
			}
			if(D[v]!=0||v==n)
				Q.push(make_pair(-(dis[v]+D[v]),v));
		}
	}
}

void init()
{
	scanf("%d %d %d",&n,&m,&L);
	for (int i=1;i<=m;i++)
		scanf("%d %d %d",&x[i],&y[i],&z[i]),add(x[i],y[i],z[i]);
	for (int i=1;i<=L;i++)
		scanf("%d",&l[i]),del[l[i]]=1,rev[x[l[i]]]=i;
	rev[n]=INF;
	for (int i=L;i>=1;i--)
		D[x[l[i]]]=D[y[l[i]]]+z[l[i]];
}

void work()
{
	for (int i=1;i<=n;i++)
		dis[i]=INF;
	dis[1]=0;
	SPFA(1);
	printf("%d\n",Q.empty()?-1:-Q.top().first);
	for (int i=1;i<L;i++)
	{
		del[l[i]]=0;
		SPFA(x[l[i]]);
		while(!Q.empty()&&rev[Q.top().second]<=i+1)
			Q.pop();
		printf("%d\n",Q.empty()?-1:-Q.top().first);
	}
}

int main()
{
	init();
	work();
	return 0;
}

标签:include,int,luogu,void,back,HNOI2014,push,P3238,dis
来源: https://www.cnblogs.com/With-penguin/p/12839141.html