sgu103 最短路

4 篇文章 0 订阅
3 篇文章 0 订阅

简略题意:
给出S和T,问从S到T的最短路。
但是两个节点能通行当且仅当两个节点的颜色相同
每个节点有一个初始颜色,当前颜色剩余的时间,以及每种颜色的持续时长。

需要注意的点:

1. 双向边
2. 无解(两种颜色无限交替)

稍微修改一下最短路,每次从一个路口走到另一个路口时,需要附加上额外的时间代价,这个代价可以通过模拟得到。

然后就是一个普通的最短路。

#define others
#ifdef poj
#include <iostream>
#include <cstring>
#include <cmath>
#include <cstdio>
#include <algorithm>
#include <vector>
#include <string>
#endif // poj
#ifdef others
#include <bits/stdc++.h>
#endif // others
//#define file
#define all(x) x.begin(), x.end()
using namespace std;
const double eps = 1e-8;
int dcmp(double x) { if(fabs(x)<=eps) return 0; return (x>0)?1:-1;};
typedef long long LL;


namespace solver {
    struct A {
        LL col;
        LL r;
        LL t[2];
        void in() {
            char c;
            scanf(" %c", &c);
            if(c == 'B') col = 0;
            else col = 1;
            scanf("%lld %lld %lld", &r, &t[0], &t[1]);
        }
    };
    int color(A x, int t) {
        int sum = x.t[0] + x.t[1];
        t = (t - x.r + sum) % sum;
        return t < x.t[x.col^1]?x.col^1:x.col;
    }
    LL wait_time(A x, A y, LL t) {
        for(int i = 0; i < 300; i++) {
            if(color(x, t+i) == color(y, t+i)) {
                return i;
            }
        }
        return 1e18;
    }
    const LL maxn = 333;
    vector<pair<LL, LL> > G[maxn];
    LL dis[maxn], vis[maxn];
    LL pre[maxn];
    LL S, T;
    LL n, m;
    void solve() {
        for(int i = 0; i < maxn; i++) dis[i] = 1e18;
        scanf("%lld%lld", &S, &T);
        scanf("%lld%lld", &n, &m);
        vector<A> vec;
        vec.resize(n + 1);
        for(LL i = 1; i <= n; i++)
            vec[i].in();
        for(LL i = 1; i <= m; i++) {
            LL u, v, w;
            scanf("%lld%lld%lld", &u, &v, &w);
            G[u].push_back({v, w});
            G[v].push_back({u, w});
        }
        queue<LL> q;
        q.push(S);
        vis[S] = 1;
        dis[S] = 0;
        while(!q.empty()) {
            LL u = q.front(); q.pop();
            vis[u] = 0;
            for(LL i = 0; i < G[u].size(); i++) {
                LL v = G[u][i].first, w = G[u][i].second;
                LL wait = wait_time(vec[u], vec[v], dis[u]);
                if(dis[v] > dis[u] + w + wait) {
                    dis[v] = dis[u] + w + wait;
                    pre[v] = u;
                    if(!vis[v]) {
                        q.push(v);
                        vis[v] = 1;
                    }
                }
            }
        }
        if(dis[T] == 1e18) {
            puts("0");
        } else {
            cout<<dis[T]<<endl;
            vector<LL> G;
            while(T) {
                G.push_back(T);
                T = pre[T];
            }
            reverse(all(G));
            for(LL i = 0; i < G.size(); i++) {
                printf("%lld%c", G[i], i == G.size()-1?'\n':' ');
            }
        }
    }
}

int main() {
#ifdef file
    freopen("gangsters.in", "r", stdin);
    freopen("gangsters.out", "w", stdout);
#endif // file
//    int t;
//    scanf("%lld", &t);
//    while(t--)
    solver::solve();
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值