Minimum Path CodeForces - 1473E
You are given a weighted undirected connected graph consisting of n vertices and m edges. It is guaranteed that there are no self-loops or multiple edges in the given graph.
Let’s define the weight of the path consisting of k edges with indices e1,e2,…,ek as
, where wi — weight of the i-th edge in the graph.
Your task is to find the minimum weight of the path from the 1-st vertex to the i-th vertex for each i (2≤i≤n).
Input
The first line contains two integers n and m (2≤n≤2⋅105; 1≤m≤2⋅105) — the number of vertices and the number of edges in the graph.
Following m lines contains three integers vi,ui,wi (1≤vi,ui≤n; 1≤wi≤109; vi≠ui) — endpoints of the i-th edge and its weight respectively.
Output
Print n−1 integers — the minimum weight of the path from 1-st vertex to the i-th vertex for each i (2≤i≤n).
Examples
Input
5 4
5 3 4
2 1 1
3 2 2
2 4 2
Output
1 2 2 4
Input
6 8
3 1 1
3 6 2
5 4 2
4 2 2
6 1 1
5 2 1
3 2 3
1 5 4
Output
2 1 4 3 1
Input
7 10
7 5 5
2 3 3
4 7 1
5 3 6
2 7 6
6 2 6
3 7 6
4 2 1
3 1 4
1 7 4
Output
3 4 2 7 7 3
题意:
求最短路,最短路的长度是边权之和减去最长边长度并加上最短边长度。
方法1:
通过记录状态求最短距离,使用dis[maxn][2][2]表示最短距离,dis[i][1][1]表示已完成两个操作。
求此最短路必须使用 将一条边权值变为0 和 将一条边权值*2 两个操作各一次。
最终结果要最短,肯定是将最大边权变为0,最小边权*2,直接跑最短路即可。
#include <iostream>
#include <cmath>
#include <string>
#include <queue>
#include <cstring>
#include <list>
#include <map>
#include <set>
#include <vector>
#include <algorithm>
#include <iomanip>
#include <fstream>
#include <cstdlib>
#include <sstream>
#include <stack>
#define fcio ios::sync_with_stdio(false);cin.tie(0);cout.tie(0)
#define eps 1e-12
#define MEM(a,b) memset((a),(b),sizeof(a))
using namespace std;
typedef long long ll;
const int maxn = 2e5 + 10;
const int inf = 0x3f3f3f3f;
const ll INF = 0x3f3f3f3f3f3f3f3f;
template <typename T>
inline void read(T& X);
class edge {
public:
int to;
ll cost;
};
class node {
public:
int to;
ll cost;
int a, b;// a max变0 b min*2
bool operator<(const node& a)const {
return cost > a.cost;
}
};
int n, m;
vector<edge>G[maxn];
ll dis[maxn][2][2];
bool vis[maxn][2][2];
void dij() {
MEM(dis, INF);
MEM(vis, 0);
dis[1][0][0] = 0;
priority_queue<node>que;
que.push({ 1,0,0,0 });
while (que.size()) {
node ths = que.top();
que.pop();
int a = ths.a, b = ths.b;
if (vis[ths.to][a][b])continue;
vis[ths.to][a][b] = 1;
for (auto i : G[ths.to]) {
if (dis[i.to][a][b] > dis[ths.to][a][b] + i.cost) {
dis[i.to][a][b] = dis[ths.to][a][b] + i.cost;
que.push({ i.to,dis[i.to][a][b],a,b });
}
if (!a && dis[i.to][1][b] > dis[ths.to][a][b]) {
dis[i.to][1][b] = dis[ths.to][a][b];
que.push({ i.to,dis[i.to][1][b],1,b });
}
if (!b && dis[i.to][a][1] > dis[ths.to][a][b] + i.cost*2) {
dis[i.to][a][1] = dis[ths.to][a][b] + i.cost * 2;
que.push({ i.to,dis[i.to][a][1],a,1 });
}
if (!a && !b && dis[i.to][1][1] > dis[ths.to][a][b] + i.cost) {
dis[i.to][1][1] = dis[ths.to][a][b] + i.cost;
que.push({ i.to,dis[i.to][1][1],1,1 });
}
}
}
}
int main()
{
fcio;
while (cin >> n >> m) {
for (int i = 0; i <= n; i++)G[i].clear();
for (int i = 0; i < m; i++) {
int a, b; ll c;
cin >> a >> b >> c;
G[a].push_back({ b,c });
G[b].push_back({ a,c });
}
dij();
for (int i = 2; i <= n; i++) {
cout << dis[i][1][1] << (i == n ? '\n' : ' ');
}
}
return 0;
}
template <typename T>
inline void read(T& X) {
X = 0; int w = 0; char ch = 0;
while (!isdigit(ch)) { w |= ch == '-'; ch = getchar(); }
while (isdigit(ch)) X = (X << 3) + (X << 1) + (ch ^ 48), ch = getchar();
if (w) X = -X;
}
方法2:
建分层图跑最短路。
https://blog.csdn.net/Fighting_Peter/article/details/112666260
有空再补