G. Reducing Delivery Cost(最短路)
Codeforces Round #677 (Div. 3) G. Reducing Delivery Cost
題目大意
n個點,m條邊,k個路徑。你可以選擇刪除圖中的一條邊,問你刪除後k個路徑的長度和最小值。
思路
先 n 2 l o g n n^2logn n2logn處理出所有路徑。
然後列舉m條邊去掉其中一條後的最小路徑值和。
#include<bits/stdc++.h>
using namespace std;
const int maxn=1010;
typedef long long ll;
int n,m,k;
int head[maxn],tot=0;
struct edge{
int to,next,val;
}edge[maxn*maxn];
int ux[maxn*maxn],uy[maxn*maxn],uw[maxn*maxn];
int gx[maxn],gy[maxn];
void add(int u,int v,int val){
edge[++tot].to=v;
edge[tot].next=head[u];
edge[tot].val=val;
head[u]=tot;
}
int f[maxn][maxn];
int dis[maxn];
bool vis[maxn];
struct node{
int val,to;
bool operator<(const node& x)const{
return x.val<val;
}
};
void dij(int src){
for(int i=1;i<maxn;++i){
vis[i]=false;dis[i]=1e9;
}
dis[src]=0;
priority_queue<node>q;
while(!q.empty())q.pop();
q.push({0,src});
while(!q.empty()){
node now=q.top();q.pop();
int u=now.to;
if(vis[u])continue;
vis[u]=1;
for(int i=head[u];i;i=edge[i].next){
int v=edge[i].to;
if(dis[v]>dis[u]+edge[i].val){
dis[v]=dis[u]+edge[i].val;
q.push({dis[v],v});
}
}
}
for(int i=1;i<=n;++i){
f[src][i]=dis[i];
}
}
int main(){
cin>>n>>m>>k;
for(int i=1;i<=m;++i){
int u,v,val;cin>>u>>v>>val;
add(u,v,val);
add(v,u,val);
ux[i]=u,uy[i]=v,uw[i]=val;
}
vector<pair<int,int>>v;
for(int i=1;i<=k;++i){
int u,v;cin>>u>>v;
gx[i]=u,gy[i]=v;
}
for(int i=1;i<=n;++i){
dij(i);
}
ll sum=1e18;
for(int i=1;i<=m;++i){
ll ans=0;
int x=ux[i],y=uy[i],w=uw[i];
for(int j=1;j<=k;++j){
int u=gx[j],v=gy[j];
ans+=min({f[u][x]+f[v][y],f[u][y]+f[v][x],f[u][v]});
}
sum=min(sum,ans);
}
cout<<sum<<endl;
}
相關文章
- 最短路:求最長最短路
- 最短路 || 最長路 || 次短路
- G. Sakurako and Chefir
- G. GCD on a gridGC
- G. Rudolf and Subway
- G. Shuffling Songs
- G. Gifts from Knowledge
- G. D-FunctionFunction
- var let cost
- cost量化分析
- 6 Thing that determine composite cost
- What is the Average Cost of Doing a Diploma?
- HDU 1385 Minimum Transport Cost
- SAP QM Cost of Quality Inspection
- Delivery Automatic Creation for UB type STO
- CF 1971 G. XOUR (*1700) 思維
- PostgreSQL DBA(175) - Cost EST(SeqScan)SQL
- Least Cost Bracket Sequence(貪心)ASTRacket
- 746. Min Cost Climbing StairsAI
- SAP HANA Delivery Unit概念簡述
- 【CodeChef】Graph Cost(動態規劃)動態規劃
- Oracle 監聽投毒COST解決Oracle
- 最短路
- 次短路
- 最大值(最短路+最短路計數)
- [LeetCode] 857. Minimum Cost to Hire K WorkersLeetCode
- 2024_4_22 路徑花費為最長$k$條邊之和最短路
- 最短路-Floyd
- Paper Reading: Cost-sensitive deep forest for price predictionREST
- SAP MM MIGO介面裡的'Via Delivery'選項Go
- 內容分發網路(Content Delivery Network,CDN)
- 軟體定義交付宣言(Software Defined Delivery Manifesto)
- Educational Codeforces Round 95 (Rated for Div. 2) G. Three Occurrences
- 最短路專項
- 擴點最短路
- 最短路之Dijkstra
- 最短路圖論圖論
- 6.4.2最短路徑