纯属练代码の题。。orz估价函数是反向spfa后到目标点的距离
神奇的是我这渣代码竟然还是statu第12?
#include <cstdio>
#include <iostream>
#include <queue>
#include <cstring>
using namespace std;
int read()
{
int sign = 1, n = 0; char c = getchar();
while(c < '0' || c > '9'){ if(c == '-') sign = -1; c = getchar(); }
while(c >= '0' && c <= '9') { n = n*10 + c-'0'; c = getchar(); }
return sign*n;
}
const int maxN = 1005;
const int maxM = 100005;
int N, M;
int S, T, K;
struct ed{
int v, w, next;
}e[maxM * 2];
int k = 1, head[maxN], heee[maxN];
inline void adde(int u, int v, int w)
{
e[k] = (ed){ v, w, head[u] };
head[u] = k++;
e[k] = (ed){ u, w, heee[v] };
heee[v] = k++;
}
int dis[maxN];
bool vis[maxN];
queue <int> q;
inline void spfa(int s, int t)
{
memset(dis, 0x3f, sizeof(dis));
q.push(s); dis[s] = 0;
while(q.size())
{
int u = q.front(); q.pop(); vis[u] = 0;
for(int i = heee[u]; i; i = e[i].next)
{
int v = e[i].v;
if(dis[v] > dis[u] + e[i].w)
{
dis[v] = dis[u] + e[i].w;
if(!vis[v]) { vis[v] = 1; q.push(v); }
}
}
}
}
struct node{
int n, g, h;
bool operator < (const node &b) const{ return g + h > b.g + b.h; }
};
priority_queue <node> Q;
int Astar()
{
Q.push((node){ S, 0, dis[S] });
int cnt = 0;
while(Q.size())
{
node temp = Q.top(); Q.pop();
int u = temp.n;
if(u == T && ++cnt == K) return temp.g;
for(int i = head[u]; i; i = e[i].next)
{
int v = e[i].v;
node ex = (node){ v, temp.g + e[i].w, dis[v] };
Q.push(ex);
}
}
return -1;
}
int main()
{
N = read(); M = read();
for(int i = 1; i <= M; ++i)
{
int u = read(), v = read(), w = read();
adde(u, v, w);
}
S = read(); T = read(); K = read();
spfa(T, S); if(T == S) ++K;
printf("%d\n", Astar());
return 0;
}