http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1444
题意:
思路:
哇,思路爆炸。
因为每条边的权值都为1,所以可以直接用bfs来求出任意两个点之间的最短距离,复杂度为$O(n^2)$。
然后之后再暴力枚举一下,看看这两条路径之间是否会有重复的边。
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
#include<set>
using namespace std;
typedef long long ll;
const int INF = 0x3f3f3f3f;
const int maxn=+; int n, m;
vector<int> G[maxn];
int vis[maxn];
int d[maxn][maxn];
int s1,t1,l1,s2,t2,l2; void bfs()
{
// memset(d,0,sizeof(d));
for(int i=;i<=n;i++)
{
memset(vis,,sizeof(vis));
queue<int> Q;
Q.push(i);
vis[i]=;
while(!Q.empty())
{
int u=Q.front(); Q.pop();
for(int j=;j<G[u].size();j++)
{
int v=G[u][j];
if(!vis[v])
{
vis[v]=;
d[i][v]=d[i][u]+;
Q.push(v);
}
}
}
}
} bool judge(int s1, int t1, int s2, int t2, int i, int j)
{
return d[s1][i] + d[i][j] + d[j][t1] <= l1 && d[s2][i] + d[i][j] + d[j][t2] <= l2;
} int main()
{
//freopen("in.txt","r",stdin);
scanf("%d%d",&n,&m);
for(int i=;i<=m;i++)
{
int u,v;
scanf("%d%d",&u,&v);
G[u].push_back(v);
G[v].push_back(u);
} scanf("%d%d%d",&s1,&t1,&l1);
scanf("%d%d%d",&s2,&t2,&l2); bfs(); int ans=d[s1][t1]+d[s2][t2];
if(d[s1][t1]>l1 || d[s2][t2]>l2) puts("-1");
else
{
for(int i=;i<=n;i++)
for(int j=;j<=n;j++)
{
if (judge(s1, t1, s2, t2, i, j))
{
ans = min(ans, d[s1][i] + d[i][j] + d[j][t1] + d[s2][i] + d[j][t2]);
}
if (judge(t1, s1, t2, s2, i, j))
{
ans = min(ans, d[t1][i] + d[i][j] + d[j][s1] + d[t2][i] + d[j][s2]);
}
if (judge(s1, t1, t2, s2, i, j))
{
ans = min(ans, d[s1][i] + d[i][j] + d[j][t1] + d[t2][i] + d[j][s2]);
}
if (judge(t1, s1, s2, t2, i, j))
{
ans = min(ans, d[t1][i] + d[i][j] + d[j][s1] + d[s2][i] + d[j][t2]);
}
}
printf("%d\n",m-ans);
}
return ;
}