题意:
一幅无向图,100000个点,200000条边,问从起点到终点的最短路(不存在输出 -1)。但有一个限制条件:在这幅图中有一个magic门,如果人到达这个门的起点,那么人会自动并立即到达这个magic门的终点,这个magic门的有效次数是K(100000)次。
分析:
假设这个magic门的起点和终点分别用A B表示,那么我们围绕这个门的使用次数进行讨论即可:
情况一:使用0次。这种情况下,A这个点是无用的,所以我们把A点的所有出边全部清空,然后直接求 st 到 en 的最短路即可。
情况二:使用1次。此时A点也无用,也把A点的所有出边全部清空,然后求 st 到 A 的最短路,加上 en 到 B 的最短路,即为答案。
情况三:使用K次。其实,A点还是无用的!把A点的所有出边全部清空。此时从 B 到 A 肯定是走最短路,所以先求出此最短路,然后答案就是st 到A,加上en 到A,再加上K*dis[A]。
所以,综合来看,A这个点始终是个无用点,那么在最开始直接把它的出边全部清空。然后再分别讨论上述3种情况并更新答案即可。
// #define debug
//#define file
double abcxx00;
#define FF cout<<"==================================input:", cin>>abcxx00;
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<sstream>
#include<string>
#include<cmath>
#include<cctype>
#include<cstdlib>
#include<vector>
#include<queue>
#include<stack>
#include<list>
#include<set>
#include<map>
#define mp make_pair
#define pb push_back
#define fi first
#define se second
using namespace std;
typedef long long LLL;
const LLL maxn = (5+ 1e5);
const LLL inf_LLL = 1e18;
typedef pair<LLL,LLL> pii; ///adjid,len
int n,m;
vector<pii>adj[maxn];
LLL st,en,A,B,K,en00;
LLL dis[maxn];
bool inque[maxn];
queue<int>que;
void spfa(int S)
{
LLL e,a,b,len,s,f;
for(e=0; e <= n; e++)
{
inque[e] = false;
dis[e] = (e==S ? 0: inf_LLL);
}
while(!que.empty())
que.pop();
que.push(S);
while(!que.empty())
{
a=que.front(), que.pop(), inque[a]=false;
for(s=adj[a].size(),f=0; f < s; f++)
{
b = adj[a].at(f).fi;
len = adj[a].at(f).se;
if(dis[b] > len+dis[a])
{
dis[b] = len+dis[a];
if(!inque[b])
{
inque[b] = true;
que.push(b);
}
}
}
}
}
LLL cyh()
{
adj[A].clear();
LLL ans=inf_LLL, stA,stB,enA,enB;
spfa(st), stA=dis[A], stB=dis[B];
ans = min(ans, dis[en00]);
spfa(en00), enA=dis[A], enB=dis[B];
ans = min(ans, stA+enB);
//ans = min(ans, stB+enB);
spfa(B);
if(dis[A] != inf_LLL)
{
//ans = min(ans, dis[A]*(K+1)+stB+enA);
ans = min(ans, dis[A]*K+stA+enA);
}
return ans==inf_LLL ? -1 : ans;
}
int main()
{
//freopen("data99.in", "r", stdin);
//freopen("A.out", "w", stdout);
int test,cas=0,e,a,b,len;
for(cin>>test; test--; )
{
cin>>n>>m; n++;
for(e=0; e <= n; e++)
adj[e].clear();
while(m--)
{
scanf("%d %d %d", &a,&b,&len);
if(a != b)
adj[a].pb(mp(b,len)), adj[b].pb(mp(a,len));
}
cin>>st>>en>>A>>B>>K; en00=n-1;
adj[en].pb(mp(en00,0)), adj[en00].pb(mp(en,0));
printf("Case #%d: ", ++cas);
cout << cyh() << endl;
}
return 0;
}
奉上我的数据:
15
4 4
0 1 10
1 2 10
2 3 10
0 3 20
0 3 2 1 10
4 3
0 1 10
1 2 10
2 3 10
0 3 2 1 10
4 2
0 1 10
2 3 20
0 3 1 2 10
4 0
0 3 1 2 10
4 2
0 1 10
2 3 20
0 3 2 1 10
2 1
0 1 123
0 1 0 1 100000
3 2
0 1 11
1 2 22
0 2 0 1 1
2 1
0 1 123
0 1 1 0 100000
10 9
0 1 100000
1 2 100000
2 3 100000
3 4 100000
4 5 100000
5 6 100000
6 7 100000
7 8 100000
8 9 100000
0 9 9 0 99995
4 4
2 0 202020
0 1 1
3 0 303030
1 3 3
2 3 1 0 5
4 4
2 0 202020
0 1 1
3 0 303030
1 3 333333
2 3 1 0 5
4 4
0 1 1000000
2 3 1000000
0 2 1
1 2 1
0 3 2 1 1
4 4
0 2 222
0 1 1
1 3 7
0 3 333
2 3 0 1 5
4 3
0 1 3
1 2 1
1 3 7
0 3 1 2 5
4 4
0 2 1
0 1 3
1 2 1
1 3 70
0 3 1 2 100
答案:
Case #1: 20
Case #2: 130
Case #3: 30
Case #4: -1
Case #5: -1
Case #6: 0
Case #7: 22
Case #8: 12300123
Case #9: 89996400000
Case #10: 202029
Case #11: 505050
Case #12: 1000002
Case #13: 229
Case #14: 15
Case #15: 172