【题解】codeforces25C Roads in Berland

There are n cities numbered from 1 to n in Berland. Some of them are connected by two-way roads. Each road has its own length — an integer number from 1 to 1000. It is known that from each city it is possible to get to any other city by existing roads. Also for each pair of cities it is known the shortest distance between them. Berland Government plans to build k new roads. For each of the planned road it is known its length, and what cities it will connect. To control the correctness of the construction of new roads, after the opening of another road Berland government wants to check the sum of the shortest distances between all pairs of cities. Help them — for a given matrix of shortest distances on the old roads and plans of all new roads, find out how the sum of the shortest distances between all pairs of cities changes after construction of each road.

Input
The first line contains integer n (2 ≤ n ≤ 300) — amount of cities in Berland. Then there follow n lines with n integer numbers each — the matrix of shortest distances. j-th integer in the i-th row — di, j, the shortest distance between cities i and j. It is guaranteed that di, i = 0, di, j = dj, i, and a given matrix is a matrix of shortest distances for some set of two-way roads with integer lengths from 1 to 1000, such that from each city it is possible to get to any other city using these roads.

Next line contains integer k (1 ≤ k ≤ 300) — amount of planned roads. Following k lines contain the description of the planned roads. Each road is described by three space-separated integers ai, bi, ci (1 ≤ ai, bi ≤ n, ai ≠ bi, 1 ≤ ci ≤ 1000) — ai and bi — pair of cities, which the road connects, ci — the length of the road. It can be several roads between a pair of cities, but no road connects the city with itself.

Output
Output k space-separated integers qi (1 ≤ i ≤ k). qi should be equal to the sum of shortest distances between all pairs of cities after the construction of roads with indexes from 1 to i. Roads are numbered from 1 in the input order. Each pair of cities should be taken into account in the sum exactly once, i. e. we count unordered pairs.

Examples
input
2
0 5
5 0
1
1 2 3
output
3
input
3
0 4 5
4 0 9
5 9 0
2
2 3 8
1 2 1
output
17 12

【题解】
本题要求的是任意两点之间的最短路,且n ≤ 300,想到用floyd
不过不能O(n^3k)死暴力,会Tle的

所以我们要加个小优化,因为每次只更新一条边,所以每次floyd也只更新与更新的边有关的边。即,若a到b的边为输入的,那么以a和b为中转站去更新其他点,时间复杂度O(n^2k)

Code:

#include <bits/stdc++.h>
#define res register int
#define ll long long
#define maxn 1010
using namespace std;
ll dis[maxn][maxn];
int n,m;

inline int read(){
    int s=0,w=1;
    char c=getchar();
    while (c<'0' || c>'9'){if (c=='-')w=-1;c=getchar();}
    while (c>='0' && c<='9') s=s*10+c-'0',c=getchar();
    return s*w;
}
int main(){
    n=read();
    for (res i=1;i<=n;++i)
        for (res j=1;j<=n;++j) dis[i][j]=read();
    m=read();
    while (m--){
        int x=read(),y=read(),z=read();
        if (z<dis[x][y]){
            dis[x][y]=dis[y][x]=z;
            for (res i=1;i<=n;++i)
                for (res j=1;j<=n;++j)
                    if (i!=j){
                        dis[i][j]=min(dis[i][j],min(dis[i][x]+dis[x][j],dis[i][y]+dis[y][j]));
                        dis[j][i]=dis[i][j];
                    }
        }
        ll ans=0;
        for (res i=1;i<=n;++i)
            for (res j=1;j<i;++j) ans+=dis[i][j];
        printf("%lld ",ans);
    }
    return 0;
}
### Codeforces 题目解答思路与方法 #### Monsters and Spells 的解答思路 对于题目 *Monsters And Spells* ,其核心在于模拟怪物受到伤害的过程并判断终能否击败所有怪物。此过程涉及到贪心算法的应用,具体来说是在每一轮攻击中尽可能多地减少怪物的生命值。 为了实现这一目标,可以先按照怪物初始生命值降序排列,然后依次处理每一个怪物,在每次施放技能时优先选择能造成大伤害的方式。通过这种方式能够确保在有限的能量下大化总伤害输出[^1]。 ```cpp #include <bits/stdc++.h> using namespace std; int main() { int t; cin >> t; while(t--) { long long n, h, a, b, k; cin >> n >> h >> a >> b >> k; vector<pair<long long,int>> monsters(n); for(int i = 0; i < n; ++i){ cin >> monsters[i].first; // 生命值 monsters[i].second = i; } sort(monsters.rbegin(), monsters.rend()); // 按照生命值从高到低排序 bool canDefeatAll = true; for(auto& m : monsters) { if(m.first > someFunctionToCalculateDamage(h,a,b,k)){ canDefeatAll = false; break; } } cout << (canDefeatAll ? "YES\n" : "NO\n"); } } ``` 上述代码片段展示了如何读取输入数据并对怪物按生命值进行排序,之后遍历这些已排序的数据来决定是否有可能战胜所有的敌人。 #### Sequence 数字序列生成逻辑分析 针对 *Sequence* 这一问题,则采取了一种完全不同的策略。考虑到直接计算会遇到性能瓶颈以及难以预测的结果模式,转而探索是否存在周期性的特性成为了解决方案的关键所在。经过观察发现随着数值的增长确实出现了重复现象,这意味着一旦找到了这样的循环节就可以快速定位任意位置上的元素而不必逐项构建整个列表[^3]。 ```python def find_nth_number(n): sequence = [] current_num = 1 seen = {} while True: str_form = ''.join(sorted(str(current_num))) if str_form in seen: loop_start_index = seen[str_form] non_loop_part_length = len(sequence[:loop_start_index]) relative_position_within_cycle = (n - non_loop_part_length - 1) % \ (len(sequence) - non_loop_part_length) return int(''.join(sorted(str(sequence[relative_position_within_cycle])))) seen[str_form] = len(sequence) sequence.append(current_num) next_value_options = set([current_num * 2, int(''.join(sorted(str(current_num))))]) current_num = min(next_value_options.difference(set(sequence)), default=current_num + 1) print(find_nth_number(15)) # 输出应为1156 ``` 这段 Python 实现首先尝试建立直到检测到第一个重复项为止的部分序列;接着利用模运算找到给定索引 `n` 对应在环内的确切位置,并据此返回相应的整数值。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值