Codeforces 544D Destroying Roads

本文解析了Codeforces B题“Destroying Roads”的算法思路,介绍了如何在保证两点间距离限制的同时,最大化删除边数的问题。通过SPFA算法求最短路径,考虑路径相交情况,优化边数保留策略。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

链接:http://codeforces.com/problemset/problem/543/B

                    B. Destroying Roads

time limit per test:2 seconds

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 alongthe roads.

You want to destroy the largest possible number of roads in the country

so that the remainingroads would allow you to get from city s1 to city t

in at most l1 hours and get from city s2 tocity t2 in at most l2 hours.

Determine what maximum number of roads you need to destroy in order

to meet the conditionof 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 cityto 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, l

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

题意:

        给出一张n个点,m条边的无向图,问最多能删除几条边,

同时保证给定点之间的距离<=某一值。

       or 给定n个点m条边的无向图(边权全为1),让你去掉最多的边使得d(s1, t1) <= l1

&& d(s2, t2) <= l2,若不能满足输出-1,反之输出可以去掉的最多边数。

Examples

思路:

        例如给出的两条限制路径为a->...->b,c->...->d。如果这两条路径不相交的话,

显然去掉的最多边数=m-dist【a】【b】-dist【c】【d】。dist可以用spfa求一下最短路,

边权均为1。别的肯定会都超时,spfa有不超时的可能。

        然后当两条路径之间有公共路径时,我们将公共路径作为中间点更新最小保留边数,

又因为n只有10^3,所以可以两重循环枚举所有边,不是最短路中公共边的边产生的

答案肯定要比最优解大的,所以只是多几个无用的冗余判断,不影响最终结果。其次

注意的是四个点之间的不同的位置关系有两种,分别更新一下答案即可。

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

 

AC代码:

#include<bits/stdc++.h>
using namespace std;
typedef long long LL;
#define N 3005
#define inf 0x3f3f3f3f
struct Edge
{
    int to,next,w;
}edge[N*N];
int cnt,n,head[N],low[N][N],w[N];
bool vis[N];
inline void add(int u,int v,int w)
{
    edge[cnt].to=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}
void spfa()
{
    int i,j;
    for(j=1; j<=n; ++j){
        memset(vis,0,sizeof(vis));
        low[j][j]=0;
        vis[j]=1;
        queue<int> q;
        q.push(j);
        while(!q.empty()){
            int u=q.front();
            q.pop();
            vis[u]=0;
            for(i=head[u]; ~i; i=edge[i].next){
                int v=edge[i].to;
                if(low[j][v]>low[j][u]+edge[i].w){
                    low[j][v]=low[j][u]+edge[i].w;
                    if(!vis[v]){
                        vis[v]=1;
                        q.push(v);
                    }
                }
            }
        }
    }
}
int main()
{
    int m,i,j,x,y,s1,t1,s2,t2,l1,l2;
    cin>>n>>m;
    memset(head,-1,sizeof(head));
    memset(low,inf,sizeof(low));
    for(i=1;i<=m;++i){
        scanf("%d%d",&x,&y);
        add(x,y,1);
        add(y,x,1);
    }
    spfa();
    scanf("%d%d%d%d%d%d",&s1,&t1,&l1,&s2,&t2,&l2);
    if(low[s1][t1]>l1||low[s2][t2]>l2) puts("-1");
    else{
        int ans=low[s1][t1]+low[s2][t2];
        for(i=1;i<=n;++i)
            for(j=1;j<=n;++j)
            {
                if(low[s1][i]+low[i][j]+low[j][t1]<=l1&&low[s2][i]+low[i][j]+low[j][t2]<=l2)
                    ans=min(ans,low[s1][i]+low[i][j]+low[j][t1]+low[s2][i]+low[j][t2]);
                if(low[s1][i]+low[i][j]+low[j][t1]<=l1&&low[t2][i]+low[i][j]+low[j][s2]<=l2)
                    ans=min(ans,low[s1][i]+low[j][t1]+low[t2][i]+low[i][j]+low[j][s2]);
                
            }
        printf("%d\n",m-ans);
    }
    return 0;
}

 

The end;

### Codeforces 1487D Problem Solution The problem described involves determining the maximum amount of a product that can be created from given quantities of ingredients under an idealized production process. For this specific case on Codeforces with problem number 1487D, while direct details about this exact question are not provided here, similar problems often involve resource allocation or limiting reagent type calculations. For instance, when faced with such constraints-based questions where multiple resources contribute to producing one unit of output but at different ratios, finding the bottleneck becomes crucial. In another context related to crafting items using various materials, it was determined that the formula `min(a[0],a[1],a[2]/2,a[3]/7,a[4]/4)` could represent how these limits interact[^1]. However, applying this directly without knowing specifics like what each array element represents in relation to the actual requirements for creating "philosophical stones" as mentioned would require adjustments based upon the precise conditions outlined within 1487D itself. To solve or discuss solutions effectively regarding Codeforces' challenge numbered 1487D: - Carefully read through all aspects presented by the contest organizers. - Identify which ingredient or component acts as the primary constraint towards achieving full capacity utilization. - Implement logic reflecting those relationships accurately; typically involving loops, conditionals, and possibly dynamic programming depending on complexity level required beyond simple minimum value determination across adjusted inputs. ```cpp #include <iostream> #include <vector> using namespace std; int main() { int n; cin >> n; vector<long long> a(n); for(int i=0;i<n;++i){ cin>>a[i]; } // Assuming indices correspond appropriately per problem statement's ratio requirement cout << min({a[0], a[1], a[2]/2LL, a[3]/7LL, a[4]/4LL}) << endl; } ``` --related questions-- 1. How does identifying bottlenecks help optimize algorithms solving constrained optimization problems? 2. What strategies should contestants adopt when translating mathematical formulas into code during competitive coding events? 3. Can you explain why understanding input-output relations is critical before implementing any algorithmic approach? 4. In what ways do prefix-suffix-middle frameworks enhance model training efficiency outside of just tokenization improvements? 5. Why might adjusting sample proportions specifically benefit models designed for tasks requiring both strong linguistic comprehension alongside logical reasoning skills?
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值