题目链接
http://codeforces.com/gym/101630/attachments
题解
zyb学长的题。
先枚举第(k)大的边权,设其边权为(x),然后把每条边边权减掉(x)与(0)取(max), 跑最短路之后加上(x imes k)更新答案。
注意从(0)开始枚举(就相当于裸跑最短路)。
代码
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cassert>
#include<algorithm>
#include<queue>
#include<cstring>
#define llong long long
using namespace std;
const int N = 3000;
const int M = 6000;
struct Edge
{
int v,nxt; llong w;
} e[M+3];
int fe[N+3];
struct AEdge
{
int u,v; llong w;
} ae[M+3];
struct DijNode
{
int u; llong dis;
DijNode() {}
DijNode(int _u,llong _dis) {u = _u,dis = _dis;}
bool operator <(const DijNode &arg) const {return dis>arg.dis;}
};
priority_queue<DijNode> pq;
llong dis[N+3];
bool vis[N+3];
int n,en,p,s,t,m;
void addedge(int u,int v,llong w)
{
en++; e[en].v = v; e[en].w = w;
e[en].nxt = fe[u]; fe[u] = en;
}
void clear()
{
for(int i=1; i<=m; i++) e[i].v = e[i].nxt = e[i].w = 0;
for(int i=1; i<=n; i++) fe[i] = 0;
en = 0;
}
llong Dijkstra()
{
memset(dis,42,sizeof(dis)); memset(vis,false,sizeof(vis));
dis[s] = 0ll; pq.push(DijNode(s,0));
while(!pq.empty())
{
DijNode tmp = pq.top(); pq.pop(); int u = tmp.u;
if(tmp.dis!=dis[u]) continue;
if(vis[u]==true) continue;
vis[u] = true;
for(int i=fe[u]; i; i=e[i].nxt)
{
int v = e[i].v;
if(dis[v]>dis[u]+e[i].w && vis[v]==false)
{
dis[v] = dis[u]+e[i].w;
pq.push(DijNode(v,dis[v]));
}
}
}
return dis[t];
}
int main()
{
scanf("%d%d%d",&n,&m,&p); s = 1; t = n;
for(int i=1; i<=m; i++)
{
scanf("%d%d%I64d",&ae[i].u,&ae[i].v,&ae[i].w);
}
llong ans = 10000000000000000ll;
for(int i=0; i<=m; i++)
{
for(int j=1; j<=m; j++) addedge(ae[j].u,ae[j].v,max(ae[j].w-ae[i].w,0ll)),addedge(ae[j].v,ae[j].u,max(ae[j].w-ae[i].w,0ll));
llong cur = Dijkstra();
ans = min(ans,cur+ae[i].w*p);
clear();
}
printf("%I64d
",ans);
return 0;
}