[Codeforces Round #677 (Div. 3)] G. Reducing Delivery Cost (dijkstra,枚举)
[Codeforces Round #677 (Div. 3)] G. Reducing Delivery Cost (dijkstra,枚举)
题面:
题意:
给定一个含有\(\mathit n\)个点\(\mathit m\)个边的图,和\(\mathit k\)个点对,让你选择一个边将其权值变为0,使其\(\sum\limits_{i=1}^{k} d(a_i, b_i)\) 最小,其中\(d(x,y)\) 代表图中\(x->y\) 的最短路径值。
思路:
首先\(O(n^2logn)\) 可以处理出原图中任意两点之间的最短路\(dis[x][y]\)。
然后我们枚举每一个边\((x_i,y_i)\),计算如果该边权值变为0后的\(\sum\limits_{i=1}^{k} d(a_i, b_i)\),
其中对于每一个\(d(a_i,b_i)=min(dis[a_i][b_i],dis[x_i][a_i]+dis[y_i][b_i],dis[x_i][b_i]+dis[y_i][a_i])\)。
对于每一个边算出的答案取最小值即可。
代码:
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <bits/stdc++.h>
#define ALL(x) (x).begin(), (x).end()
#define sz(a) int(a.size())
#define rep(i,x,n) for(int i=x;i<n;i++)
#define repd(i,x,n) for(int i=x;i<=n;i++)
#define pii pair<int,int>
#define pll pair<long long ,long long>
#define gbtb ios::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define MS0(X) memset((X), 0, sizeof((X)))
#define MSC0(X) memset((X), '\0', sizeof((X)))
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define eps 1e-6
#define chu(x) if(DEBUG_Switch) cout<<"["<<#x<<" "<<(x)<<"]"<<endl
#define du3(a,b,c) scanf("%d %d %d",&(a),&(b),&(c))
#define du2(a,b) scanf("%d %d",&(a),&(b))
#define du1(a) scanf("%d",&(a));
using namespace std;
typedef long long ll;
ll gcd(ll a, ll b) {return b ? gcd(b, a % b) : a;}
ll lcm(ll a, ll b) {return a / gcd(a, b) * b;}
ll powmod(ll a, ll b, ll MOD) { if (a == 0ll) {return 0ll;} a %= MOD; ll ans = 1; while (b) {if (b & 1) {ans = ans * a % MOD;} a = a * a % MOD; b >>= 1;} return ans;}
ll poww(ll a, ll b) { if (a == 0ll) {return 0ll;} ll ans = 1; while (b) {if (b & 1) {ans = ans * a ;} a = a * a ; b >>= 1;} return ans;}
void Pv(const vector<int> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%d", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
void Pvl(const vector<ll> &V) {int Len = sz(V); for (int i = 0; i < Len; ++i) {printf("%lld", V[i] ); if (i != Len - 1) {printf(" ");} else {printf("\n");}}}
inline long long readll() {long long tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
inline int readint() {int tmp = 0, fh = 1; char c = getchar(); while (c < '0' || c > '9') {if (c == '-') { fh = -1; } c = getchar();} while (c >= '0' && c <= '9') { tmp = tmp * 10 + c - 48, c = getchar(); } return tmp * fh;}
void pvarr_int(int *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%d%c", arr[i], i == n ? '\n' : ' ');}}
void pvarr_LL(ll *arr, int n, int strat = 1) {if (strat == 0) {n--;} repd(i, strat, n) {printf("%lld%c", arr[i], i == n ? '\n' : ' ');}}
const int maxn = 1010;
const int inf = 0x3f3f3f3f;
/*** TEMPLATE CODE * * STARTS HERE ***/
#define DEBUG_Switch 0
struct node {
int to;
ll val;
node() {}
node(int tt, ll vv)
{
to = tt;
val = vv;
}
bool operator < (const node &b) const
{
return val > b.val;
}
};
std::vector<node> e[maxn];
ll dis[maxn][maxn];
void addedge(int a, int b, ll v)
{
e[a].push_back(node(b, v));
e[b].push_back(node(a, v));
}
bool vis[maxn];
void init(int n, int st)
{
for (int i = 1; i <= n; ++i) {
// dis[i]=inf;
dis[st][i] = 1e18;
vis[i] = 0;
}
}
priority_queue<node> heap;
int n;
void dijkstra(int strat)
{
init(n, strat);
dis[strat][strat] = 0ll;
heap.push(node(strat, 0ll));
node temp;
while (!heap.empty()) {
temp = heap.top();
heap.pop();
if (vis[temp.to]) {
continue;
} else {
vis[temp.to] = 1;
}
for (auto &x : e[temp.to]) {
if ( dis[strat][temp.to] + x.val < dis[strat][x.to]) {
dis[strat][x.to] = dis[strat][temp.to] + x.val;
heap.push(node(x.to, dis[strat][x.to]));
}
}
}
}
int m, k;
pii info[maxn];
pii ee[maxn];
int main()
{
#if DEBUG_Switch
freopen("D:\\code\\input.txt", "r", stdin);
#endif
//freopen("D:\\code\\output.txt","w",stdout);
n = readint(); m = readint(); k = readint();
repd(i, 1, m) {
int u = readint();
int v = readint();
int c = readint();
addedge(u, v, c);
ee[i].fi = u; ee[i].se = v;
}
repd(i, 1, n) {
dijkstra(i);
}
repd(i, 1, k) {
info[i].fi = readint(); info[i].se = readint();
}
int ans = inf;
repd(i, 1, m) {
int temp = 0;
repd(j, 1, k) {
temp += min(dis[info[j].fi][info[j].se], min(dis[ee[i].fi][info[j].fi] + dis[ee[i].se][info[j].se], dis[ee[i].fi][info[j].se] + dis[ee[i].se][info[j].fi]));
}
ans = min(ans, temp);
}
printf("%d\n", ans );
return 0;
}
本博客为本人原创,如需转载,请必须声明博客的源地址。
本人博客地址为:www.cnblogs.com/qieqiemin/
希望所写的文章对您有帮助。