4289: PA2012 Tax
Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 271 Solved: 90
[ Submit][ Status][ Discuss]
Description
给出一个N个点M条边的无向图,经过一个点的代价是进入和离开这个点的两条边的边权的较大值,求从起点1到点N的最小代价。起点的代价是离开起点的边的边权,终点的代价是进入终点的边的边权
N<=100000
M<=200000
Input
Output
Sample Input
4 5
1 2 5
1 3 2
2 3 1
2 4 4
3 4 8
1 2 5
1 3 2
2 3 1
2 4 4
3 4 8
Sample Output
12
HINT
Source
一个点的点权是由出入边共同决定,不好统计
先将原图转化,点->边,边->点,每条边又拆成两个点
对于每个点,将从它连出去的所有出边,按照权值升序排好,做差分
相邻边连边,权值小到大的边权为差分值,从大到小的,边权为0
对于每条边拆分的两个点,分别连权值为边权的边
考虑在新图中跑最短路,,从某个出边代表的点,转移到它的反向边
通过差分的权值进行方向调整,于是这样就能跑出原图的最短路了
画个图比较好理解,最后起点和终点的边权注意特判
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
#include<vector>
#include<queue>
#include<ext/pb_ds/priority_queue.hpp>
using namespace std;
const int maxn = 1E5 + 10;
const int maxm = 2E5 + 20;
const int N = maxm * 2;
typedef long long LL;
const LL INF = 1E16;
struct E{
int to,w; E(){}
E(int to,int w): to(to),w(w){}
};
struct data{
int Num; LL d; data(){}
data(int Num,LL d): Num(Num),d(d){}
bool operator < (const data &B) const {return d > B.d;}
};
typedef __gnu_pbds::priority_queue<data,less<data>,__gnu_pbds::pairing_heap_tag> Heap;
int n,m,tot = 2,x[maxm],y[maxm],w[maxm],A[maxm],B[maxm];
LL dis[N]; bool inq[N];
vector <E> v[N];
vector <int> G[maxn];
Heap Q; Heap::point_iterator id[N];
bool cmp(const int &a,const int &b) {return w[a] < w[b];}
int getint()
{
char ch = getchar(); int ret = 0;
while (ch < '0' || '9' < ch) ch = getchar();
while ('0' <= ch && ch <= '9')
ret = ret*10 + ch - '0',ch = getchar();
return ret;
}
int main()
{
#ifdef DMC
freopen("DMC.txt","r",stdin);
#endif
n = getint(); m = getint();
for (int i = 1; i <= m; i++)
{
A[i] = tot++; B[i] = tot++;
x[i] = getint(); y[i] = getint(); w[i] = getint();
G[x[i]].push_back(i); G[y[i]].push_back(i);
v[A[i]].push_back(E(B[i],w[i]));
v[B[i]].push_back(E(A[i],w[i]));
}
for (int i = 2; i < n; i++)
{
if (!G[i].size()) continue;
sort(G[i].begin(),G[i].end(),cmp);
int last = x[G[i][0]] == i ? A[G[i][0]] : B[G[i][0]];
for (int j = 1; j < G[i].size(); j++)
{
int now = x[G[i][j]] == i ? A[G[i][j]] : B[G[i][j]];
v[last].push_back(E(now,w[G[i][j]] - w[G[i][j-1]]));
v[now].push_back(E(last,0)); last = now;
}
}
for (int i = 0; i < G[1].size(); i++)
{
int k = x[G[1][i]] == 1 ? A[G[1][i]] : B[G[1][i]];
v[1].push_back(E(k,w[G[1][i]]));
}
for (int i = 0; i < G[n].size(); i++)
{
int k = y[G[n][i]] == n ? A[G[n][i]] : B[G[n][i]];
v[k].push_back(E(tot,w[G[n][i]]));
}
id[1] = Q.push(data(1,0)); inq[1] = 1;
for (int i = 2; i <= tot; i++) dis[i] = INF;
while (!Q.empty())
{
int k = Q.top().Num; Q.pop();
for (int i = 0; i < v[k].size(); i++)
{
E e = v[k][i];
if (dis[e.to] > dis[k] + 1LL * e.w)
{
dis[e.to] = dis[k] + 1LL * e.w;
if (!inq[e.to]) inq[e.to] = 1,id[e.to] = Q.push(data(e.to,dis[e.to]));
else Q.modify(id[e.to],data(e.to,dis[e.to]));
}
}
}
cout << dis[tot] << endl;
return 0;
}