题意#
A国有N座城市,依次标为1到N。同时,在这N座城市间有M条单向道路,每条道路的长度是一个正整数。现在,A国交通部指定了一条从城市1到城市N的路径,并且保证这条路径的长度是所有从城市1到城市N的路径中最短的。不幸的是,因为从城市1到城市N旅行的人越来越多,这条由交通部指定的路径经常发生堵塞。现在A国想知道,这条路径中的任意一条道路无法通行时,由城市1到N的最短路径长度是多少。
2
<
N
<
100000
,
1
<
M
<
200000
2<N<100000,1<M<200000
2<N<100000,1<M<200000。所用道路长度大于0小于10000。
分析
首先要知道最短路上去掉一条边后的最短路一定是s->x->…->y->t这样的形式(x,y分别是原来最短路上的点,也有可能直接就是s->…->t)。 那么我们考虑从起点到终点的顺序开始删边。每删掉一条边后就把该边的起点加到队列里面然后跑spfa,并且钦定其不能走删掉的边和最短路上的边。若走到一个最短路上的点且其标号大于起点则将这条路径加入一个堆里面,最后取堆顶即可。若堆为空则输出-1. 然后每次跑完spfa之后dis数组是不用清空的,因为可以注意到,如果不能跑最短路上的边的话,那么随着删的边不断地变化,dis数组也是不递增的,所以就直接在原来的dis数组上跑即可。 那么我们只要预处理出起点到每个最短路上的点的距离和每个点到终点的距离即可。
再不懂就看程序吧。。。
话说这样的题对拍估计比正解还要难打一些吧。。。
代码
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std
;
typedef long long LL
;
const int N
=100005;
const int M
=200005;
int n
,m
,l
,a
[M
],cnt
,last
[N
],num
[N
],vis
[N
],now
,id
[N
],tim
,sta
[N
],top
;
LL dis
[N
],tot
[N
],val
[N
],pre
[N
];
struct edge
{int to
,next
,from
,len
,tag
,use
;}e
[M
];
struct data
{
int x
;LL dis
;
bool operator
< (const data
&a
) const
{
return dis
>a
.dis
;
}
};
queue
<int> q
;
priority_queue
<data
> que
;
int read()
{
int x
=0,f
=1;char ch
=getchar();
while (ch
<'0'||ch
>'9'){if(ch
=='-')f
=-1;ch
=getchar();}
while (ch
>='0'&&ch
<='9'){x
=x
*10+ch
-'0';ch
=getchar();}
return x
*f
;
}
void addedge(int u
,int v
,int len
)
{
e
[++cnt
].from
=u
;e
[cnt
].to
=v
;e
[cnt
].len
=len
;e
[cnt
].next
=last
[u
];last
[u
]=cnt
;
}
void get_tag(int x
,int tag
)
{
num
[x
]=tag
;
for (int i
=last
[x
];i
;i
=e
[i
].next
)
if (e
[i
].tag
)
{
pre
[e
[i
].to
]=pre
[x
]+e
[i
].len
;
get_tag(e
[i
].to
,tag
+1);
tot
[x
]=tot
[e
[i
].to
]+e
[i
].len
;
break;
}
}
void spfa()
{
tim
++;
while (!q
.empty())
{
int u
=q
.front();q
.pop();
for (int i
=last
[u
];i
;i
=e
[i
].next
)
{
if (e
[i
].use
) continue;
if (num
[e
[i
].to
]>=now
)
{
if (id
[e
[i
].to
]!=tim
) id
[e
[i
].to
]=tim
,sta
[++top
]=e
[i
].to
,val
[e
[i
].to
]=dis
[u
]+e
[i
].len
+tot
[e
[i
].to
];
else val
[e
[i
].to
]=min(val
[e
[i
].to
],dis
[u
]+e
[i
].len
+tot
[e
[i
].to
]);
}
else if (dis
[u
]+e
[i
].len
<dis
[e
[i
].to
])
{
dis
[e
[i
].to
]=dis
[u
]+e
[i
].len
;
if (!vis
[e
[i
].to
]) vis
[e
[i
].to
]=1,q
.push(e
[i
].to
);
}
}
vis
[u
]=0;
}
while (top
)
{
data u
;u
.x
=sta
[top
];u
.dis
=val
[sta
[top
]];
que
.push(u
);
top
--;
}
}
void solve()
{
for (int i
=2;i
<=n
;i
++) dis
[i
]=1e15;
for (int i
=1;i
<=l
;i
++)
{
int x
=e
[a
[i
]].from
,y
=e
[a
[i
]].to
;
now
=num
[y
];e
[a
[i
]].use
=1;
q
.push(x
);vis
[x
]=1;dis
[x
]=pre
[x
];spfa();
if (que
.empty())
{
printf("-1\n");
e
[a
[i
]].use
=0;
continue;
}
data u
=que
.top();
while (num
[u
.x
]<=num
[x
])
{
que
.pop();
if (que
.empty()) break;
u
=que
.top();
}
if (num
[u
.x
]>num
[x
]) printf("%lld\n",u
.dis
);
else printf("-1\n");
e
[a
[i
]].use
=0;
}
}
int main()
{
n
=read();m
=read();l
=read();
for (int i
=1;i
<=m
;i
++)
{
int x
=read(),y
=read(),len
=read();
addedge(x
,y
,len
);
}
for (int i
=1;i
<=l
;i
++) a
[i
]=read(),e
[a
[i
]].tag
=1;
get_tag(1,1);
solve();
return 0;
}