51 nod 1444 破坏道路 (枚举最短路)

在某一个国家,那儿有n个城市,他们通过m条双向道路相连。城市从1到n编号。如果城市a和b通过一条道路直接相连,那么他们之间的距离就是一个小时。这个国家的道路网络可以允许你从任意一个城市到达另外的城市。

现在你要破坏尽可能多的道路,但是要保证从城市s1到t1不超过l1小时,并且从城市s2到t2不超过l2小时。

输出最多可以破坏的道路数目,如果没有解,请输出-1

 

Input

单组测试数据。
第一行有两个整数n,m(1 ≤ n ≤ 3000, n-1 ≤ m ≤ min(3000,n*(n-1)/2) )。
接下来m行,每行有两个整数 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ bi),表示ai和bi之间有一条道路。
输入保证是一个连通图。
最后两行每行有三个整数s1, t1, l1和 s2, t2, l2, (1 ≤ si, ti ≤ n, 0 ≤ li ≤ n)。

Output

输出一个整数,表示最多可以破坏的道路数目,如果没有解,输出-1。

Input示例

5 4
1 2
2 3
3 4
4 5
1 3 2
3 5 2

Output示例

0

求出任意两点之间最短路,然后暴力枚举所有可能重叠部分,求出最小ans即可.

#pragma GCC optimize(2)
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<queue>
using namespace std;
namespace fastIO {
#define BUF_SIZE 100000
	//fread -> read
	bool IOerror = 0;
	inline char nc() {
		static char buf[BUF_SIZE], *p1 = buf + BUF_SIZE, *pend = buf + BUF_SIZE;
		if (p1 == pend) {
			p1 = buf;
			pend = buf + fread(buf, 1, BUF_SIZE, stdin);
			if (pend == p1) {
				IOerror = 1;
				return -1;
			}
		}
		return *p1++;
	}
	inline bool blank(char ch) {
		return ch == ' ' || ch == '\n' || ch == '\r' || ch == '\t';
	}
	inline void read(int &x) {
		char ch;
		while (blank(ch = nc()));
		if (IOerror) return;
		for (x = ch - '0'; (ch = nc()) >= '0' && ch <= '9'; x = x * 10 + ch - '0');
	}
#undef BUF_SIZE
};
using namespace fastIO;
const int maxn = 3500;
const int inf = 0x3f3f3f3f;
int n, m;
vector<int>vec[maxn];
int dis[maxn][maxn];
bool vis[maxn];
int s1, t1, l1, s2, t2, l2;
void bfs()
{
	for (int i = 1; i <= n; i++)
	{
		memset(vis, false, sizeof(vis));
		queue<int>pq;
		pq.push(i);
		vis[i] = true;
		while (!pq.empty())
		{
			int u = pq.front();
			pq.pop();
			for (int j = 0; j < (int)vec[u].size(); j++)
			{
				int v = vec[u][j];
				if (!vis[v])
				{
					vis[v] = true;
					dis[i][v] = dis[i][u] + 1;
					pq.push(v);
				}
			}
		}
	}
}
bool charge(int s1, int t1, int s2, int t2, int i, int j)
{
	return dis[s1][i] + dis[i][j] + dis[j][t1] <= l1 && dis[s2][i] + dis[i][j] + dis[j][t2] <= l2;
}
int main()
{
	//freopen("C://input.txt", "r", stdin);
	read(n), read(m);
	for (int i = 0; i < m; i++)
	{
		int u, v;
		read(u), read(v);
		vec[u].push_back(v);
		vec[v].push_back(u);
	}
	read(s1), read(t1), read(l1), read(s2), read(t2), read(l2);
	bfs();
	int ans = dis[s1][t1] + dis[s2][t2];
	if (dis[s1][t1] > l1 || dis[s2][t2] > l2)
	{
		printf("-1\n");
	}
	else
	{
		for (int i = 1; i <= n; i++)
		{
			for (int j = 1; j <= n; j++)
			{
				if (charge(s1, t1, s2, t2, i, j))
				{
					ans = min(ans, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[s2][i] + dis[j][t2]);
				}
				if (charge(t1, s1, s2, t2, i, j))
				{
					ans = min(ans, dis[t1][i] + dis[i][j] + dis[j][s1] + dis[s2][i] + dis[j][t2]);
				}
				if (charge(s1, t1, t2, s2, i, j))
				{
					ans = min(ans, dis[s1][i] + dis[i][j] + dis[j][t1] + dis[t2][i] + dis[j][s2]);
				}
				if (charge(t1, s1, t2, s2, i, j))
				{
					ans = min(ans, dis[t1][i] + dis[i][j] + dis[j][s1] + dis[t2][i] + dis[j][s2]);
				}
			}
		}
		printf("%d\n", m - ans);
	}
	return 0;
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值