zoukankan      html  css  js  c++  java
  • Codeforces Round #302 (Div. 2) D. Destroying Roads 最短路

    题目链接:

    题目

    D. Destroying Roads
    time limit per test 2 seconds
    memory limit per test 256 megabytes
    inputstandard input
    outputstandard output

    问题描述

    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.

    输入

    The first line contains two integers n, m (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 ai, bi (1 ≤ ai, bi ≤ n, ai ≠ 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).

    输出

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

    样例

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

    output
    0

    题意

    给你一个无向图,要求从s1到t1的距离要小于等于l1从s2到t2的距离小于等于l2
    问你能删除最多多少条边。

    题解

    首先跑任意两点之间的最短路。
    如果两条路完全没有公共边,则答案为m-d[s1][t1]-d[s2][t2]。
    如果两条边有公共边,则路径一定为“H"形状的,我们可以通过枚举公共边集的两个端点i,j,暴力所有的情况。
    官方题解

    代码

    #include<iostream>
    #include<cstring>
    #include<cstdio>
    #include<algorithm>
    #include<vector>
    #include<string>
    #include<queue>
    using namespace std;
    
    const int maxn = 3e3+10;
    int n, m;
    
    vector<int> G[maxn];
    int d[maxn][maxn];
    int inq[maxn];
    void spfa(int s) {
    	memset(inq, 0, sizeof(inq));
    	queue<int> Q;
    	d[s][s] = 0, inq[s] = 1, Q.push(s);
    	while (!Q.empty()) {
    		int u = Q.front(); Q.pop();
    		inq[u] = 0;
    		for (int i = 0; i < G[u].size(); i++) {
    			int v = G[u][i];
    			if (d[s][v] > d[s][u] + 1) {
    				d[s][v] = d[s][u] + 1;
    				if (!inq[v]) inq[v] = 1, Q.push(v);
    			}
    		}
    	}
    }
    
    void init() {
    	memset(d, 0x3f, sizeof(d));
    }
    
    int main() {
    	scanf("%d%d", &n, &m);
    	init();
    	for (int i = 0; i < m; i++) {
    		int u, v;
    		scanf("%d%d", &u, &v), u--,v--;
    		G[u].push_back(v);
    		G[v].push_back(u);
    	}
    	for (int i = 0; i < n; i++) {
    		spfa(i);
    	}
    	int s1, t1, l1, s2, t2, l2;
    	scanf("%d%d%d", &s1, &t1, &l1),s1--,t1--;
    	scanf("%d%d%d", &s2, &t2, &l2),s2--,t2--;
    	if (d[s1][t1]>l1 || d[s2][t2]>l2) {
    
    		printf("-1
    "); return 0;
    	}
    	int ans = d[s1][t1] + d[s2][t2];
    	for (int i = 0; i < n; i++) {
    		for (int j = 0; j < n; j++) {
    			if (i == j) continue;
    			if (d[s1][i] + d[i][j] + d[j][t1] <= l1&&d[s2][i] + d[i][j] + d[j][t2] <= l2) {
    				ans = min(ans, d[s1][i] + d[s2][i] + d[i][j] + d[j][t1] + d[j][t2]);
    			}
    			if (d[s1][i] + d[i][j] + d[j][t1] <= l1&&d[s2][j] + d[j][i] + d[i][t2] <= l2) {
    				ans = min(ans, d[s1][i] + d[i][j] + d[j][t1] + d[s2][j] + d[i][t2]);
    			}
    		}
    	}
    	printf("%d
    ", m - ans);
    	return 0;
    }
  • 相关阅读:
    shell 时间循环
    t
    IntelliJ IDEA For Mac 快捷键
    JVM的默认参数
    qt不同模块使用多语言
    cocos2dx 实现gpu instancing
    so so.*.*
    Android开发-解决 AIDL 中找不到couldn't find import for class错误
    Android Watchdog源码简析--Based on Android 6.0.1
    View绘制流程--Based on kitkat
  • 原文地址:https://www.cnblogs.com/fenice/p/5635767.html
Copyright © 2011-2022 走看看