SDUT ACM 2622 最短路径 二维SPFA启蒙题。。 Anti

 

最短路径

Time Limit: 1000ms   Memory limit: 65536K  有疑问?点这里^_^

题目描述

为了准备一年一度的校赛,大家都在忙着往赛场搬运东西,比如气球什么的。这时 YY 也没有闲着,他也加入了搬运工的行列。已知学校有 个路口和 条路,YY 并不是把东西直接搬到赛场,而是从 路口搬运到 路口。由于 YY 非常懒而且他有轻度强迫症。所以他要走的路需要尽可能的短,并且走路径 的倍数。

输入

 

输入的第一行为一个正整数T(1  T  20),代表测试数据组数。

对于每组测试数据:

输入的第一行为两个正整数  M N  100, 1  M  10000)。

接下来M行每行三个正整数 UVW0  UV < N, 0  W  230 ),代表有一条从UV的长度为W有向路径

最后一行为三个正整数SX ST N, 1   10)。

输出

 

对于每组测试数据,输出满足条件的从  的最短路径。如果从  不可达,或者无法满足路径数是 的倍数,输出No Answer!”(不包含引号)

注意:64-bit 整型请使用 long long 来定义,并且使用 %lld  cincout 来输入输出,请不要使用 __int64  %I64d

示例输入

2
2 1
0 1 1
0 1 2
3 2
0 1 1
1 2 1
0 2 2

示例输出

No Answer!
2

提示

 

来源

 “师创杯”山东理工大学第五届ACM程序设计竞赛
 
 
#include <queue>
#include <vector>
#include <cstdio>
#include <cstring>
using namespace std;
const long long INF = 1LL << 40;
long long dist[110][12];
bool inque[110];
struct edge{
    int to, w;
};
int n, m, s, t, x;
vector<struct edge>map[110];
void spfa()
{
    queue<int>q;
    for(int i = 0; i < n; i++)
        for(int k = 0; k <= x; k++)
            dist[i][k] = INF;
    dist[s][0] = 0;
    memset(inque, 0, sizeof(inque));
    q.push(s);
    inque[s] = 1;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        inque[u] = 0;
        for(int i = 0; i < map[u].size(); i++)
        {
            int to = map[u][i].to;
            for(int k = 0; k < x; k++)
            {
                if(dist[u][k] < INF && dist[to][(k+1)%x] > dist[u][k] + map[u][i].w)
                {
                    dist[to][(k+1)%x] = dist[u][k] + map[u][i].w;
                    if(!inque[to])
                    {
                        q.push(to);
                        inque[to] = 1;
                    }
                }
            }
        }
    }
}
int main()
{
    int item, u, v, w;
    struct edge tmp;
    scanf("%d", &item);
    while(item--)
    {
        scanf("%d %d", &n, &m);
        for(int i = 0; i < n; i++)
            map[i].clear();
        while(m--)
        {
            scanf("%d %d %d", &u, &v, &w);
            tmp.to = v;
            tmp.w = w;
            map[u].push_back(tmp);
        }
        scanf("%d %d %d", &s, &t, &x);
        spfa();
        if(dist[t][0] >= INF)
            printf("No Answer!\n");
        else
            printf("%lld\n", dist[t][0]);
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wolfred7464/p/3080249.html