Codeforces544D:Destroying Roads(最短路)

In some country there are exactly n cities and m bidirectional
roads connecting the cities. Cities are numbered with integers from 1 to n.
If cities a and b are
connected by a road, then in an hour you can go along this road either from city a to city b,
or from city b to city a.
The road network is such that from any city you can get to any other one by moving along the roads.

You want to destroy the largest possible number of roads in the country so that the remaining roads would allow you to get from city s1 to
city t1 in
at most l1 hours
and get from city s2 to
city t2 in
at most l2 hours.

Determine what maximum number of roads you need to destroy in order to meet the condition of your plan. If it is impossible to reach the desired result, print -1.

Input

The first line contains two integers nm (1?≤?n?≤?3000, ) —
the number of cities and roads in the country, respectively.

Next m lines contain the descriptions of the roads as pairs of integers aibi (1?≤?ai,?bi?≤?nai?≠?bi).
It is guaranteed that the roads that are given in the description can transport you from any city to any other one. It is guaranteed that each pair of cities has at most one road between them.

The last two lines contains three integers each, s1, t1, l1 and s2, t2, l2,
respectively (1?≤?si,?ti?≤?n, 0?≤?li?≤?n).

Output

Print a single number — the answer to the problem. If the it is impossible to meet the conditions, print -1.

Sample test(s)

input

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

output

0

input

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

output

1

input

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

output

-1

参考的这篇博客:http://www.cnblogs.com/wuyuewoniu/p/4491935.html


#include <iostream>
#include <stdio.h>
#include <string.h>
#include <stack>
#include <queue>
#include <map>
#include <set>
#include <vector>
#include <math.h>
#include <bitset>
#include <algorithm>
#include <climits>
using namespace std;

#define LS 2*i
#define RS 2*i+1
#define UP(i,x,y) for(i=x;i<=y;i++)
#define DOWN(i,x,y) for(i=x;i>=y;i--)
#define MEM(a,x) memset(a,x,sizeof(a))
#define W(a) while(a)
#define LL long long
#define N 5005
#define MOD 19999997
#define INF 0x3f3f3f3f
#define EXP 1e-8
const double Pi = acos(-1.0);

vector<int> G[N];
int dis[N][N],n,m;
int vis[N];
int s1,s2,t1,t2,d1,d2;

int main()
{
    int i,j,k,x,y;
    cin>>n>>m;
    UP(i,1,m)
    {
        scanf("%d%d",&x,&y);
        G[x].push_back(y);
        G[y].push_back(x);
    }
    cin>>s1>>t1>>d1>>s2>>t2>>d2;
    MEM(dis,0);
    UP(i,1,n)
    {
        queue<int> Q;
        MEM(vis,0);
        vis[i] = 1;
        Q.push(i);

        W(!Q.empty())
        {
            int u = Q.front(),v;
            Q.pop();
           for(j = 0;j<G[u].size();j++)
            {
                v = G[u][j];
                if(vis[v]) continue;
                vis[v] = 1;
                dis[i][v] = dis[i][u]+1;
                Q.push(v);
            }
        }
    }
    if(dis[s1][t1]>d1||dis[s2][t2]>d2)
    {
        printf("-1\n");
        return 0;
    }
    int ans = dis[s1][t1]+dis[s2][t2];
    UP(i,1,n)
    {
        UP(j,1,n)
        {
            if(dis[s1][i]+dis[i][j]+dis[j][t1]<=d1&&dis[s2][i]+dis[i][j]+dis[j][t2]<=d2)
                ans=min(ans,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[s2][i]+dis[j][t2]);
            if(dis[s1][i]+dis[i][j]+dis[j][t1]<=d1&&dis[t2][i]+dis[i][j]+dis[j][s2]<=d2)
                ans=min(ans,dis[s1][i]+dis[i][j]+dis[j][t1]+dis[t2][i]+dis[j][s2]);
        }
    }
    printf("%d\n",m-ans);

    return 0;
}

时间: 2024-11-06 03:54:20

Codeforces544D:Destroying Roads(最短路)的相关文章

Codeforces 543B Destroying Roads(最短路)

题意: 给定一个n个点(n<=3000)所有边长为1的图,求最多可以删掉多少条边后,图满足s1到t1的距离小于l1,s2到t2的距离小于l2. Solution: 首先可以分两种情况讨论: 1:假设最后留下的边是互不相交的两条路径.此时的答案是n-s1到t1的最短路径-s2到t2的最短路径. 2:假设最后留下的边有重合的一段,此时只要枚举重合的这一段的起点和终点,就可以判断.注意此时要考虑这两条路径经过枚举的两点的顺序. 限制的条件比较多,可以用来剪枝的条件也很多. 由于所有边的长度为1,用DF

Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路 删边

题目:有n个城镇,m条边权为1的双向边让你破坏最多的道路,使得从s1到t1,从s2到t2的距离分别不超过d1和d2. #include <iostream> #include <cstdio> #include <cstring> #include <cstdlib> #include <cmath> #include <vector> #include <queue> #include <stack> #in

codeforces 544 D Destroying Roads 【最短路】

题意:给出n个点,m条边权为1的无向边,破坏最多的道路,使得从s1到t1,s2到t2的距离不超过d1,d2 因为最后s1,t1是连通的,且要破坏掉最多的道路,那么就是求s1到t1之间的最短路 用bfs求出任意两个顶点之间的距离, 如果d[s1][t1]>d1||d[s2][t2]>d2,那么不可能 然后就枚举重叠的区间(就像题解里面说的"H"形一样) 如果枚举区间是1--n,1--i的话,需要交换四次 如果枚举区间是1--n,1--n的话,则只需要交换一次就可以了 看的这一

【poj 1724】 ROADS 最短路(dijkstra+优先队列)

ROADS Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 12436 Accepted: 4591 Description N cities named with numbers 1 - N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that

LightOJ1002---Country Roads (最短路变形)

I am going to my home. There are many cities and many bi-directional roads between them. The cities are numbered from 0 to n-1 and each road has a cost. There are m roads. You are given the number of my city t where I belong. Now from each city you h

Gym - 100338C Important Roads 最短路+tarjan

题意:给你一幅图,问有多少条路径使得去掉该条路后最短路发生变化. 思路:先起始两点求两遍单源最短路,利用s[u] + t[v] + G[u][v] = dis 找出所有最短路径,构造新图.在新图中找到所有的桥输出就可以了. 1 #include <iostream> 2 #include <cstdio> 3 #include <fstream> 4 #include <algorithm> 5 #include <cmath> 6 #inclu

POJ 1724 ROADS 最短路

题目大意:有两个权值的最短路问题,要求满足费用不超过一定限度的情况下的最短路. 思路:正常的SPFA加一个小判断,就是当费用高于预期费用的时候不入队,顺便加一个pq吧. CODE: #include <queue> #include <cstdio> #include <cstring> #include <iostream> #include <algorithm> #define MAX 100005 #define INF 0x3f3f3f

Codeforces Round #Pi (Div. 2) E. President and Roads (最短路+强连通求割边)

题目地址:codeforces #pi (DIV2) E 题目很水..就是先求两边最短路,然后把可能为最短路的边挑出来,然后判断是否yes只需要转化成无向图跑一遍tarjan,找出割边,割边就是yes,然后剩下的边就让它的值为最短路-1就行了,如果-1后变成了非正数,就是no. 但是!!!居然卡spfa!!那是不是说cf以后就不能用可以卡的算法了..完全可以出组数据来卡这些算法...比如spfa,isap... 于是为了这题,又看了一遍迪杰斯特拉算法.. 代码如下: #include <cstd

Codeforces543 B. Destroying Roads

传送门:>Here< 题意:给出一张无向图(边权为1),并给出两对起点和终点以及距离:s1,t1,l1; s2,t2,l2; 要求删除尽量多的边,使得dis(s1,t1)<=l1, dis(s2,r2)<=l2 解题思路 首先我们会发现,由于边权都为1,删去一些边,某两点间的最短路肯定会随着删的边越来越多而越来越长(捷径被删了) 因此我们会发现,要让边删的尽量多,最好最后只剩下最短路,其它边都剩下.换句话说,如果两条最短路不相交,那么最后只会剩下孤零零的两条链 于是我们会发现,我们