题意
给定 n = 1 0 3 , m = 1 0 3 n=10^3,m=10^3 n=103,m=103的图,路径前k大边的和为该路径的代价,问其中所有从 1 − n 1-n 1−n的路径中代价的最小值是都少。
题解
- 假如所求的最短代价路径的边数小于等于k,那么答案肯定是原图最短路
- 假如所求的最短代价路径的边数大于k,那么我们发现其计算的方式是将不在前k大路径的代价置为0去计算。考虑这一问题,我们只要枚举所有边,假设该边为所求最小代价的路径中前k大的最小边,将图中所有边权减去枚举到的边权值(出现负数置0)再跑一遍最短路,结果就是
d
i
s
n
+
k
⋅
w
e
i
i
dis_n+k \cdot wei_i
disn+k⋅weii。
证明:- 假如枚举到的边比当前边小,那答案必定是 上述计算得到的答案+尾部没被置0的值必定大于上述计算所求
- 假如枚举到的边比当前边大,那答案相当于把原来后几位比当前边小的边多加了变成了当前边,答案肯定也会变大。
代码
/**
* author: TelmaZzzz
* create: 2019-08-31-15.34.35
**/
#include <map>
#include <set>
#include <ctime>
#include <cmath>
#include <queue>
#include <stack>
#include <ctime>
#include <string>
#include <vector>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <iostream>
#include <algorithm>
//#include <random>
using namespace std;
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/priority_queue.hpp>
using namespace __gnu_pbds;
typedef long long ll;
typedef unsigned long long ull;
typedef double db;
void _R(int &x) { scanf("%d", &x); }
void _R(ll &x) { scanf("%lld", &x); }
void _R(db &x) { scanf("%lf", &x); }
void _R(char &x) { scanf(" %c", &x); }
void _R(char *x) { scanf("%s", x); }
void R() {}
template<class T, class... U> void R(T &head, U &... tail) { _R(head); R(tail...); }
void _W(const int &x) { printf("%d", x); }
void _W(const ll &x) { printf("%lld", x); }
void _W(const db &x) { printf("%.16f", x); }
void _W(const char &x) { putchar(x); }
void _W(const char *x) { printf("%s", x); }
template<class T> void _W(const vector<T> &x) { for (auto i = x.begin(); i != x.end(); _W(*i++)) if (i != x.cbegin()) putchar(' '); }
void W() {}
template<class T, class... U> void W(const T &head, const U &... tail) { _W(head); putchar(sizeof...(tail) ? ' ' : '\n'); W(tail...); }
#define rep(x,y,z) for(int x=y;x<=z;x++)
#define erp(x,y,z) for(int x=y;x>=z;x--)
#define PB push_back
#define MP make_pair
#define INF 1073741824
#define inf 1152921504606846976
#define pi 3.14159265358979323846
//#pragma comment(linker,"/STACK:10240000,10240000")
//mt19937 rand_(time(0));
const int N=3007,M=1e5+7;
const long long mod=1e9+7;
inline int read(){int ret=0;char ch=getchar();bool f=1;for(;!isdigit(ch);ch=getchar()) f^=!(ch^'-');for(;isdigit(ch);ch=getchar()) ret=(ret<<1)+(ret<<3)+ch-48;return f?ret:-ret;}
ll gcd(ll a,ll b){return b?gcd(b,a%b):a;}
ll ksm(ll a,ll b,ll mod){int ans=1;while(b){if(b&1) ans=(ans*a)%mod;a=(a*a)%mod;b>>=1;}return ans;}
ll inv2(ll a,ll mod){return ksm(a,mod-2,mod);}//逆元
ll wei[M];
int head[N],NEXT[M],ver[M],tot;void link(int u,int v,ll w){ver[++tot]=v;NEXT[tot]=head[u];head[u]=tot;wei[tot]=w;}
struct node{
int u,v;
ll w;
}edge[N];
int n,m,k;
ll dis[N];
bool vis[N];
__gnu_pbds::priority_queue<pair<ll,int> >q;
void build(ll x){
rep(i,1,m){
wei[i*2]=max(edge[i].w-x,0LL);
wei[i*2+1]=max(edge[i].w-x,0LL);
}
}
void dij(){
rep(i,1,n) dis[i]=inf,vis[i]=false;
while(q.size())q.pop();
q.push(MP(0,1));
dis[1]=0;
while(q.size()){
int x=q.top().second;
q.pop();
if(x==n) return;
if(vis[x]) continue;
vis[x]=true;
for(int i=head[x];i;i=NEXT[i]){
int y=ver[i];
if(dis[y]>dis[x]+wei[i]){
dis[y]=dis[x]+wei[i];
q.push(MP(-dis[y],y));
}
}
}
}
int main(){
//freopen("1.txt","r",stdin);
//ios::sync_with_stdio(false);
int u,v;
ll w;
R(n,m,k);
tot=1;
rep(i,1,m){
R(edge[i].u,edge[i].v,edge[i].w);
link(edge[i].u,edge[i].v,edge[i].w);
link(edge[i].v,edge[i].u,edge[i].w);
}
build(0);
dij();
ll ans=inf;
ans=min(ans,dis[n]);
rep(i,1,m){
build(edge[i].w);
dij();
ans=min(ans,dis[n]+1LL*k*edge[i].w);
}
W(ans);
//cout << "time: " << (long long)clock() * 1000 / CLOCKS_PER_SEC << " ms" << endl;
return 0;
}