标签:efi i+1 分享 mod for tput iostream count ini
Description
Input
Output
Sample Input
6 7
1 2 1
2 3 1
3 4 2
4 5 1
5 6 1
1 3 3
4 6 3
1 6
4
1 2
1 3
4 3
6 5
Sample Output
7
6
Infinity
7
HINT
其实这题和[tjoi2012]桥基本差不多,如果不是最短路径上的边,那直接输出最短路即可。否则就按照[tjoi2012]桥那题来即可
/*program from Wolfycz*/
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<unordered_set>
#define inf 0x7f7f7f7f7f7f7f7f
using namespace std;
typedef long long ll;
typedef unsigned int ui;
typedef unsigned long long ull;
inline int read(){
int x=0,f=1;char ch=getchar();
for (;ch<'0'||ch>'9';ch=getchar()) if (ch=='-') f=-1;
for (;ch>='0'&&ch<='9';ch=getchar()) x=(x<<1)+(x<<3)+ch-'0';
return x*f;
}
inline void print(int x){
if (x>=10) print(x/10);
putchar(x%10+'0');
}
const int N=2e5,M=2e5;
int pre[(M<<1)+10],now[N+10],child[(M<<1)+10],val[(M<<1)+10];
int Fa[N+10],Lca[N+10],stack[N+10],dfn[N+10];
bool vis[N+10],flag[(M<<1)+10];
int n,m,tot,num,Max;
ll Dis,Ans[N+10];
struct MyHash{
size_t operator ()(const pair<int,int> &x)const{
return ((hash<int>()(x.first)<<1)^(hash<int>()(x.second)))>>1;
}
};
unordered_set<pair<int,int>,MyHash>st;
struct S1{
#define ls (p<<1)
#define rs (p<<1|1)
#define fa (p>>1)
struct node{
int x;ll v;
bool operator <(const node &a)const{return v<a.v;}
}Q[N+10];
int tot;
void insert(int x,ll v){
Q[++tot]=(node){x,v};
int p=tot;
while (p!=1&&Q[p]<Q[fa]) swap(Q[p],Q[fa]),p=fa;
}
void Delete(){
Q[1]=Q[tot--];
int p=1,son;
while (ls<=tot){
if (rs>tot||Q[ls]<Q[rs]) son=ls;
else son=rs;
if (Q[son]<Q[p]) swap(Q[p],Q[son]),p=son;
else break;
}
}
}Heap;
struct Segment{
ll tree[(N<<2)+10];
Segment(){memset(tree,127,sizeof(tree));}
void Add_tag(int p,ll v){tree[p]=min(tree[p],v);}
void pushdown(int p){
if (tree[p]==inf) return;
Add_tag(ls,tree[p]);
Add_tag(rs,tree[p]);
}
void Modify(int p,int l,int r,int x,int y,ll v){
if (x<=l&&r<=y){
Add_tag(p,v);
return;
}
int mid=(l+r)>>1;
if (x<=mid) Modify(ls,l,mid,x,y,v);
if (y>mid) Modify(rs,mid+1,r,x,y,v);
}
void traversals(int p,int l,int r){
if (l==r){
Ans[l]=tree[p];
return;
}
pushdown(p);
int mid=(l+r)>>1;
traversals(ls,l,mid);
traversals(rs,mid+1,r);
}
}Tree;
struct S2{
ll dis[N+10];
S2(){memset(dis,63,sizeof(dis));}
}Frw,Bck;
void join(int x,int y,int z){pre[++tot]=now[x],now[x]=tot,child[tot]=y,val[tot]=z;}
void insert(int x,int y,int z){join(x,y,z),join(y,x,z);}
void dijkstra(int x,ll *dis){
memset(vis,0,sizeof(vis));
Heap.insert(x,dis[x]=0);
while (Heap.tot){
int Now=Heap.Q[1].x;
Heap.Delete();
if (vis[Now]) continue;
vis[Now]=1;
for (int p=now[Now],son=child[p];p;p=pre[p],son=child[p]){
if (dis[son]>dis[Now]+val[p]){
dis[son]=dis[Now]+val[p];
Heap.insert(son,dis[son]);
}
}
}
}
void put_flag(int x){
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]){
if (Frw.dis[x]+val[p]+Bck.dis[son]==Dis){
st.insert(make_pair(x,son));
st.insert(make_pair(son,x));
flag[p]=flag[p+(p&1?1:-1)]=1;
put_flag(son);
return;
}
}
}
void Find_fa(int x){
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p])
if (Frw.dis[x]+val[p]==Frw.dis[son]&&!Fa[son]) Fa[son]=x,Find_fa(son);
}
void Fill(int x,int v){
if (Lca[x]) return;
Lca[x]=v;
for (int p=now[x],son=child[p];p;p=pre[p],son=child[p]) if (Fa[son]==x) Fill(son,v);
}
void Get_Lca(int x){
int top=0;
while (x){
Fill(x,x);
stack[++top]=x;
x=Fa[x];
}
for (int i=top;i;i--) dfn[stack[i]]=num++;
num--;
}
int main(){
int n=read(),m=read();
for (int i=1;i<=m;i++){
int x=read(),y=read(),z=read();
insert(x,y,z);
}
int S=read(),T=read();
dijkstra(S,Frw.dis);
dijkstra(T,Bck.dis);
Dis=Frw.dis[T];
put_flag(S);
Find_fa(S);
Get_Lca(T);
for (int i=1;i<=tot;i+=2){
if (flag[i]) continue;
int x=child[i],y=child[i+1],v=val[i];
if (Lca[x]==Lca[y]) continue;
if (dfn[Lca[x]]>dfn[Lca[y]]) swap(x,y);
ll tmp=Frw.dis[x]+v+Bck.dis[y];
Tree.Modify(1,1,num,dfn[Lca[x]]+1,dfn[Lca[y]],tmp);
}
Tree.traversals(1,1,num);
int Q=read();
for (int i=1;i<=Q;i++){
int x=read(),y=read();
if (!st.count(make_pair(x,y))){
printf("%lld\n",Dis);
continue;
}
printf(Ans[max(dfn[x],dfn[y])]==inf?"Infinity\n":"%lld\n",Ans[max(dfn[x],dfn[y])]);
}
return 0;
}
标签:efi i+1 分享 mod for tput iostream count ini
原文地址:https://www.cnblogs.com/Wolfycz/p/9745712.html