POJ 1192 限制条件最短路

通道在一定的时间开放,处理一下就是最短路了

strtok(char[], " ")还是很好用的

引用一下百度百科

strtok()用来将字符串分割成一个个片段。参数s指向欲分割的字符串,参数delim则为分割字符串中包含的所有字符。当strtok()在参数s的字符串中发现参数delim中包涵的分割字符时,则会将该字符改为\0 字符。在第一次调用时,strtok()必需给予参数s字符串,往后的调用则将参数s设置成NULL。每次调用成功则返回指向被分割出片段的指针。
    for (i = 0, p = strtok(str, " "); p != NULL; ++i)
    {
        a[i] = atoi(p);
        p = strtok(NULL, " ");
    }
 

atof(将字符串转换成浮点型数)
atoi(将字符串转换成整型数)
atol(将字符串转换成长整型数)
strtod(将字符串转换成浮点数)
strtol(将字符串转换成长整型数)
strtoul(将字符串转换成无符号长整型数)

SFPA

#include <set>
#include <map>
#include <cmath>
#include <queue>
#include <stack>
#include <string>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef  long long LL;
const double PI = acos(-1.0);

template <class T> inline  T MAX(T a, T b){if (a > b) return a;return b;}
template <class T> inline  T MIN(T a, T b){if (a < b) return a;return b;}

const int N = 111;
const int M = 11111;
const LL MOD = 1000000007LL;
const int dir[4][2] = {1, 0, -1, 0, 0, -1, 0, 1};
const int INF = 0x3f3f3f3f;

struct node
{
    int w, next, v, ways;
    int ti[20][2];
}edge[1111];

int cnt, head[55];
int n, m, s, t;
char str[1111];
int dist[55];
bool inq[55];


void add()
{
    int a[45];
    char *p;
    int i, j, k, u, v;
    for (i = 0, p = strtok(str, " "); p != NULL; ++i)
    {
        a[i] = atoi(p);
        p = strtok(NULL, " ");
    }
    edge[cnt].w = a[2];
    edge[cnt].v = a[1];
    edge[cnt].next = head[a[0]];
    a[2] = 0;
    for (j = 3, k = 0; j < i; j +=2, ++k)
    {
        edge[cnt].ti[k][0] = a[j - 1];
        edge[cnt].ti[k][1] = a[j];
    }
    if (i == j)
    {
        edge[cnt].ti[k][0] = a[j - 1];
        edge[cnt].ti[k][1] = INF;
        k++;
    }
    edge[cnt].ways = k;
    head[a[0]] = cnt++;

    edge[cnt] = edge[cnt - 1];
    edge[cnt].v = a[0];
    edge[cnt].next = head[a[1]];
    head[a[1]] = cnt++;
}

void solve()
{
    fill(dist, dist + n + 1, INF);
    memset(inq, false, sizeof(inq));
    dist[s] = 0;
    queue < int > q;
    q.push(s);
    while (!q.empty())
    {

        int u = q.front(); q.pop(); inq[u] = false;
        int i, j, k;
        for (i = head[u]; i != -1; i = edge[i].next)
        {
            for (k = 0; k < edge[i].ways; ++k)
            {
                if (edge[i].ti[k][1] >= MAX(edge[i].ti[k][0], dist[u]) + edge[i].w)
                {
                    if (MAX(edge[i].ti[k][0], dist[u]) + edge[i].w < dist[edge[i].v])
                    {
                        dist[edge[i].v] = MAX(edge[i].ti[k][0], dist[u]) + edge[i].w;
                        if (!inq[edge[i].v]) {inq[edge[i].v] = true; q.push(edge[i].v);}
                    }
                }
            }
        }
    }
    if (dist[t] == INF) printf("*\n");
    else printf("%d\n", dist[t]);
}





int main()
{
    while (scanf("%d", &n) != EOF && n)
    {
        scanf("%d%d%d", &m, &s, &t);
        int i, u, v;
        cnt = 0;
        memset(head, -1, sizeof(head));
        getchar();
        for (i = 0; i < m; ++i)
        {
            gets(str);
            add();
        }
        if (s == t) printf("0");
        else solve();
    }
    return 0;
}


 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值