题目链接:https://www.luogu.org/problemnew/show/P1268
给定所有点间的最短路求原图所有路径和
形如:
我们需要计算红边+绿边
绿边 = (红边+蓝边+紫边)/2
所求等于 (红边+蓝边+紫边)/2 + 红边
枚举
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
const int maxn = 50;
int dis[maxn][maxn], n, ans;
int main()
{
while(cin>>n && n)
{
memset(dis, 0, sizeof(dis));
ans = 0;
for(int i = 1; i < n; i++)
for(int j = i + 1; j <= n; j++)
cin>>dis[i][j], dis[j][i] = dis[i][j];
int ans = dis[1][2];
for(int i = 3; i <= n; i++)
{
int now = 0x7fffffff;
for(int j = 2; j < i; j++)
now = min(now, (dis[1][i]+dis[i][j]-dis[1][j])/2);
ans += now;
}
printf("%d
",ans);
}
}