【牛客网】KY28 I Wanna Go Home

在这里插入图片描述
这道题有个最多只能转变1次阵营的限制,由题意可知,M出发地是1号,目的地总是2号,从1->2必须转变一次阵营,所以就要求在这之前不能出现2-1的情况,出现了就不符合题目的要求,所以就在Dijkstra中判断松弛条件的时候加一个判断语句就能求解了:

#include<iostream>
#include<cstdio>
#include<vector>
#include<queue>
#include<cstring>

using namespace std;

const int N = 600 + 10;
const int INF = 0x3f3f3f3f;  //对于这个值,我设置成INT_MAX,1e6,1e8都有测试样例通不过,目前只知道0x3f3f3f3f可以通过

struct Edge{
    int to, length;
    Edge(int t, int l): to(t), length(l){}
};

struct Point{
    int number, distance;
    Point(int n, int d): number(n), distance(d){}
    bool operator<(const Point p) const{
        return distance > p.distance;  //距离小的优先级高
    }
};

vector<Edge> graph[N];
int dis[N], arr[N];

void Dijkstra(int s){
    priority_queue<Point> pq;
    dis[s] = 0;
    pq.push(Point(s, dis[s]));
    while(!pq.empty()){
        int u = pq.top().number;
        pq.pop();
        for(int i = 0; i < graph[u].size(); i++){
            int v = graph[u][i].to;
            int l = graph[u][i].length;
            if((dis[u] + l < dis[v]) && !(arr[u] == 2 && arr[v] == 1)){
                dis[v] = dis[u] + l;
                pq.push(Point(v, dis[v]));
            }
        }
    }
}

int main(){
    int n, m;
    while(scanf("%d", &n) != EOF){
        if(n == 0) break;
        memset(graph, 0, sizeof(graph));
        memset(dis, INF, sizeof(dis));
        scanf("%d", &m);
        while(m--){
            int from, to, length;
            scanf("%d%d%d", &from, &to, &length);
            graph[from].push_back(Edge(to, length));
            graph[to].push_back(Edge(from, length));
        }
        for(int i = 1; i <= n; i++) scanf("%d", &arr[i]);
        Dijkstra(1);
        if(dis[2] != INF) printf("%d\n", dis[2]);
        else printf("-1\n");
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

花无凋零之时

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值