完蛋完蛋,dijkstra都不会写了 啊啊啊啊啊
#include<bits/stdc++.h>
#define int long long
using namespace std;
constexpr int N = 5e5 + 7;
struct node{
int v, w;
friend bool operator < (node a, node b) {
return a.w > b.w;
}
};
int n, m, k, s, t, cnt, ans = 0x3f3f3f3f;
int dis[N], h[N], to[N], val[N], nxt[N];
bool vis[N];
void add(int a, int b, int c){
to[++cnt] = b;
val[cnt] = c;
nxt[cnt] = h[a];
h[a] = cnt;
}
node tmp;
priority_queue<node> q;
void dijkstra(){
for(int i = 1; i <= n; i++) dis[i] = 0x3f3f3f3f;
dis[s] = 0;
tmp.v = s, tmp.w = 0;
q.push(tmp);
while(!q.empty()){
int u = q.top().v;
q.pop();
if(vis[u]) continue;
vis[u] = 1;
for(int i = h[u]; i; i = nxt[i]){
if(dis[to[i]] > (long long)dis[u] + val[i]){
dis[to[i]] = dis[u] + val[i];
tmp.w = dis[to[i]], tmp.v = to[i];
q.push(tmp);
}
}
}
}
signed main(){
freopen("P4568.in", "r", stdin);
freopen("P4568.out", "w", stdout);
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
cin >> n >> m >> k;
cin >> s >> t;
for(int i = 0, u, v, w; i < m; i++){
cin >> u >> v >> w;
add(u, v, w);
add(v, u, w);
for(int j = 1; j <= k; j++){
add(u + j * n, v + j * n, w);
add(v + j * n, u + j * n, w);
add(u + j * n - n, v + j * n, 0);
add(v + j * n - n, u + j * n, 0);
}
}
dijkstra();
for(int j = 1; j <= k; j++){
if(dis[t + j * n] < ans){
ans = dis[t + j * n];
}
}
cout << ans << '\n';
return 0;
}
/*
5 6 1
0 4
0 1 5
1 2 5
2 3 5
3 4 5
2 3 3
0 2 100
*/
这是AC代码 这个代码没给过 但是重写的这个却过了!!!
#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 1e6 + 7;
struct Edge {
int to, next, weight;
} a[N * 20];
struct node{
int dist, id;
node() {}
node(int dist, int id) : dist(dist), id(id) {};
bool operator>(const node& other) const {
return dist > other.dist;
}
};
int n, m, k, s, t, cnt;
int head[N], vis[N], dist[N];
void addedge(int u, int v, int w) {
a[cnt].to = v;
a[cnt].weight = w;
a[cnt].next = head[u];
head[u] = cnt++;
}
void dijkstra(int s){
memset(vis, 0, sizeof(vis));
memset(dist, 0x3f, sizeof(dist));
priority_queue<node, vector<node>, greater<node> > q;
q.push(node{0, s});
dist[s] = 0;
while(!q.empty()){
node t = q.top();
q.pop();
if(vis[t.id]) continue;
vis[t.id] = 1;
for(int i = head[t.id]; i != -1; i = a[i].next){
int v = a[i].to;
if(dist[v] > dist[t.id] + a[i].weight) {
dist[v] = dist[t.id] + a[i].weight;
q.push(node{dist[v], v});
}
}
}
}
signed main(){
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
memset(head, -1, sizeof head);
cnt = 0;
cin >> n >> m >> k;
cin >> s >> t;
for(int i = 1, x, y, w; i <= m; i++){
cin >> x >> y >> w;
addedge(x, y, w);
addedge(y, x, w);
for(int j = 1; j <= k; j++){
addedge(x + j * n, y + j * n, w);
addedge(y + j * n, x + j * n, w);
addedge(x + (j - 1) * n, y + j * n, 0);
addedge(y + (j - 1) * n, x + j * n, 0);
}
}
for(int i = 1; i <= k; i++){
addedge(t + (i - 1) * n, t + i * n, 0);
}
dijkstra(s);
cout << dist[t + k * n] << '\n';
return 0;
}