bzoj3575[Hnoi2014]道路堵塞
http://www.lydsy.com/JudgeOnline/problem.php?id=3575
总赶脚第二题总是比第三题难。。。。。。
好吧,这题一点思路都没有
听说用民科可以过掉大部分数据。。。。。。
我们发现删边后的路径一定是这样的:起点->若干个原最短路径上的边->若干个非最短路径上的边->若干个原最短路径上的边->终点
我们发现其实就是不走原最短路径上的一个区间,并且删除的边一定在这个区间上
我们按顺序枚举边,然后从这条边的出发点开始跑SPFA,不走删掉的边(不要取清空dis数组,这里是关键,dis有单调性)。从该点跑到另一个最短路上的点p(p必须在所删的边之后),然后把整条路径的长度和p一起加入平衡树中。出来平衡树中取出长度最小的边,若其做对应的p点(最短路)并没有在所删的边之后,该边删除。若平衡树为空,则输出-1
太流弊啦~~~~
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
#include<cstdio> #include<cstdlib> #include<iostream> #include<fstream> #include<algorithm> #include<cstring> #include<string> #include<cmath> #include<queue> #include<stack> #include<map> #include<utility> #include<set> #include<bitset> #include<vector> #include<functional> #include<deque> #include<cctype> #include<climits> #include<complex> #include<cassert> //#include<bits/stdc++.h>适用于CF,UOJ,但不适用于poj using namespace std; typedef long long LL; typedef double DB; typedef pair<int,int> PII; typedef pair<DB,DB> PDD; typedef complex<DB> CP; typedef vector<int> VI; #define mmst(a,v) memset(a,v,sizeof(a)) #define mmcy(a,b) memcpy(a,b,sizeof(a)) #define fill(a,l,r,v) fill(a+l,a+r+1,v) #define re(i,a,b) for(i=(a);i<=(b);i++) #define red(i,a,b) for(i=(a);i>=(b);i--) #define fi first #define se second #define mp(a,b) make_pair(a,b) #define pb(a) push_back(a) #define SF scanf #define PF printf #define two(k) (1<<(k)) #define SZ(x) (int(x.size())) #define all(x) (x).begin(),(x).end() #define ire(i,v,x) for(i=0,v=i<SZ(x)?x[i]:0;i<SZ(x);v=x[++i]) template<class T>inline T sqr(T x){return x*x;} template<class T>inline void upmin(T &t,T tmp){if(t>tmp)t=tmp;} template<class T>inline void upmax(T &t,T tmp){if(t<tmp)t=tmp;} inline int sgn(DB x){if(abs(x)<1e-9)return 0;return(x>0)?1:-1;} const DB Pi=acos(-1.0); int gint() { int res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } LL gll() { LL res=0;bool neg=0;char z; for(z=getchar();z!=EOF && z!='-' && !isdigit(z);z=getchar()); if(z==EOF)return 0; if(z=='-'){neg=1;z=getchar();} for(;z!=EOF && isdigit(z);res=res*10+z-'0',z=getchar()); return (neg)?-res:res; } const int maxn=100100; const int maxm=200100; int n,m,l; int now,info[maxn]; struct Tedge{int v,cost,next;}edge[maxm]; int E[maxm],len[maxm],G[maxm],pre[maxm],suc[maxm],pos[maxn],ban[maxm],val[maxn]; void addedge(int u,int v,int cost){edge[++now]=(Tedge){v,cost,info[u]};info[u]=now;} multiset<PII> S; int dis[maxn],ci,vis[maxn]; queue<int> que; int top,sta[maxn]; void SPFA(int d,int source,int lim) { int i; while(!que.empty())que.pop(); top=0; ci++; dis[source]=d; que.push(source);vis[source]=ci; while(!que.empty()) { int u=que.front(),v,cost;que.pop(); vis[u]=0; for(i=info[u],v=edge[i].v,cost=edge[i].cost;i;i=edge[i].next,v=edge[i].v,cost=edge[i].cost)if(!ban[i]) if(pos[v]>=lim) { if(vis[v]!=ci) vis[v]=ci,sta[++top]=v,val[v]=dis[u]+cost+suc[pos[v]]; else upmin(val[v],dis[u]+cost+suc[pos[v]]); } else if(dis[u]+cost<dis[v]) { dis[v]=dis[u]+cost; if(vis[v]!=ci)que.push(v),vis[v]=ci; } } re(i,1,top)S.insert(mp(val[sta[i]],sta[i])); } int main() { freopen("road.in","r",stdin); freopen("road.out","w",stdout); int i; n=gint();m=gint(),l=gint(); re(i,1,m){int u=gint(),v=gint(),cost=gint();addedge(u,v,cost);} G[1]=1; re(i,1,l)E[i]=gint(),len[i]=edge[E[i]].cost,G[i+1]=edge[E[i]].v; re(i,1,l+1)pos[G[i]]=i; re(i,2,l+1)pre[i]=pre[i-1]+len[i-1]; red(i,l,1)suc[i]=suc[i+1]+len[i]; mmst(dis,0x3f); re(i,1,l) { ban[E[i]]=1; SPFA(pre[i],G[i],i+1); ban[E[i]]=0; while(!S.empty() && pos[S.begin()->se]<=i)S.erase(S.begin()); if(S.empty())PF("-1\n"); else PF("%d\n",S.begin()->fi); } return 0; }