SDUT 最短路径(二维SPFA)

时间:2023-12-26 12:42:55

http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=2622

 #include<stdio.h>
#include<string.h>
#include<queue>
#include<vector>
using namespace std;
const long long INF = 1LL << ;
struct node
{
int v,w;
};
vector<struct node>map[];
int inque[];
long long dis[][];
int s,t,x,n,m; void spfa()
{
queue<int>que;
for(int i = ; i < n; i++)
for(int k = ; k <= x; k++)
dis[i][k] = INF;
dis[s][] = ;
memset(inque,,sizeof(inque)); que.push(s);
inque[s] = ; while(!que.empty())
{
int u = que.front();
inque[u] = ;
que.pop();
for(int i = ; i < map[u].size(); i++)
{
int v = map[u][i].v;
for(int k = ; k < x; k++)
{
if(dis[u][k] < INF && dis[v][(k+)%x] > dis[u][k]+map[u][i].w)
{
dis[v][(k+)%x] = dis[u][k]+map[u][i].w;
if(!inque[v])
{
que.push(v);
inque[v] = ;
}
}
}
}
}
}
int main()
{
int test;
scanf("%d",&test);
while(test--)
{
scanf("%d %d",&n,&m);
for(int i = ; i < n; i++)
map[i].clear();
int u,v,w;
for(int i = ; i <= m; i++)
{
scanf("%d %d %d",&u,&v,&w);
map[u].push_back((struct node){v,w});
}
scanf("%d %d %d",&s,&t,&x);
spfa();
if(dis[t][] >= INF)
printf("No Answer!\n");
else printf("%lld\n",dis[t][]);
}
return ;
}