先跑一遍spfa,这样就排除了小于k条边的情况。枚举那k条边中最小的边权,然后让所有的边都减去它,接着跑spfa,然后在加上k*v[i]
#include <iostream> #include <cstdio> #include <queue> #include <algorithm> #include <cmath> #include <cstring> #define inf 2147483647000000 #define N 1000010 #define p(a) putchar(a) #define For(i,a,b) for(register long long i=a;i<=b;++i) using namespace std; long long n,m,k,x,y,ans; long long v[N],d[N]; bool vis[N]; deque<long long>q; struct node{ long long n; long long v; node *next; }*e[N]; inline void in(long long &x){ long long y=1;char c=getchar();x=0; while(c<'0'||c>'9'){if(c=='-')y=-1;c=getchar();} while(c<='9'&&c>='0'){ x=(x<<1)+(x<<3)+c-'0';c=getchar();} x*=y; } inline void o(long long x){ if(x<0){p('-');x=-x;} if(x>9)o(x/10); p(x%10+'0'); } inline void push(register long long x,register long long y,register long long v){ node *p; p=new node(); p->n=y; p->v=v; if(e[x]==0) e[x]=p; else{ p->next=e[x]->next; e[x]->next=p; } } inline long long spfa(long long t){ For(i,0,n) vis[i]=0,d[i]=inf; d[1]=0; q.push_back(1); while(!q.empty()){ x=q.front();q.pop_front(); vis[x]=1; for(register node *i=e[x];i;i=i->next){ if(d[i->n]>d[x]+max((long long)0,i->v-t)){ d[i->n]=d[x]+max((long long)0,i->v-t); if(!vis[i->n]){ vis[i->n]=1; if(!q.empty()&&d[i->n]<d[q.front()]) q.push_front(i->n); else q.push_back(i->n); } } } vis[x]=0; } return d[n]; } signed main(){ in(n);in(m);in(k); For(i,1,m){ in(x);in(y);in(v[i]); push(x,y,v[i]); push(y,x,v[i]); } ans=spfa(0); For(i,1,m) ans=min(ans,spfa(v[i])+k*v[i]); o(ans); return 0; }