[牛客][北大考研复试]I Wanna Go Home[dijkstra]

本文介绍了一个关于在发生内战的国家中寻找从一个城市到另一个城市的最短路径的问题,考虑到政治局势的限制,路径只能包含一条连接不同阵营城市的道路。使用Dijkstra算法并加入堆优化来解决该问题。

题目描述

    The country is facing a terrible civil war----cities in the country are divided into two parts supporting different leaders. As a merchant, Mr. M does not pay attention to politics but he actually knows the severe situation, and your task is to help him reach home as soon as possible.     "For the sake of safety,", said Mr.M, "your route should contain at most 1 road which connects two cities of different camp."     Would you please tell Mr. M at least how long will it take to reach his sweet home?

输入描述:

    The input contains multiple test cases.
    The first line of each case is an integer N (2<=N<=600), representing the number of cities in the country.
    The second line contains one integer M (0<=M<=10000), which is the number of roads.
    The following M lines are the information of the roads. Each line contains three integers A, B and T, which means the road between city A and city B will cost time T. T is in the range of [1,500].
    Next part contains N integers, which are either 1 or 2. The i-th integer shows the supporting leader of city i. 
    To simplify the problem, we assume that Mr. M starts from city 1 and his target is city 2. City 1 always supports leader 1 while city 2 is at the same side of leader 2. 
    Note that all roads are bidirectional and there is at most 1 road between two cities.
Input is ended with a case of N=0.

输出描述:

    For each test case, output one integer representing the minimum time to reach home.
    If it is impossible to reach home according to Mr. M's demands, output -1 instead.
示例1

输入

复制
2
1
1 2 100
1 2
3
3
1 2 100
1 3 40
2 3 50
1 2 1
5
5
3 1 200
5 3 150
2 5 160
4 3 170
4 2 170
1 2 2 2 1
0

输出

复制
100
90
540

 

单源最短路问题,直接套了裸的Dijkstra(按照惯例加了堆优化),更新dis时加一个2城市不能到1城市的判断即可。
#include <bits/stdc++.h>
#define inf 0x3f3f3f3f
using namespace std;
const int N = 700;
const int M = 80000;
int n,m,tol;
struct Edge{
    int v,w,next;
}edge[M];
int head[N],vis[N],dis[N];
int lable[N];
void init(){
    tol = 0;
    memset(vis,0,sizeof(vis));
    memset(head,-1,sizeof(head));
}

void addedge(int u,int v,int w){
    edge[tol] = Edge{v,w,head[u]};
    head[u] = tol++;
}
struct Node{
    int u,w;
    bool operator < (const Node a)const{
        return w > a.w;
    }
}; 
priority_queue <Node> q;
void dijkstra(){
    memset(dis,inf,sizeof(dis));
    dis[1] = 0;
    q.push(Node{1,0});
    while(!q.empty()){
        Node a = q.top();
        q.pop();
        int u = a.u,w = a.w;
        if (vis[u]) continue;
        vis[u] = 1;
        for (int i = head[u];i != -1;i = edge[i].next){
            int v = edge[i].v,w = edge[i].w;
            if (!vis[v] && dis[v] > dis[u] + w && !(lable[u] == 2 && lable[v] == 1)){
                dis[v] = dis[u] + w;
                q.push(Node{v,dis[v]});
            }
            
        }
    }
}

int main(){
    while (~scanf("%d",&n)&&n){
        scanf("%d",&m);
        init();
        for (int i = 0;i < m;++i){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            addedge(u,v,w);
            addedge(v,u,w);
        }
        for (int i = 1;i <= n;++i){
            int tmp;
            scanf("%d",&tmp);
            lable[i] = tmp;
        }
        dijkstra();
        if (dis[2] == inf) puts("-1");
            else printf("%d\n",dis[2]);
    }
}

 

转载于:https://www.cnblogs.com/mizersy/p/11613946.html

牛客是一个专注于IT领域的在线学习和求职平台。它为广大IT从业者和学习者提供了丰富的资源和多样化的功能。 从引用中可知,牛客有练习赛和算法题目相关内容。牛客练习赛会提供不同类型的算法题目,如牛客练习赛123包含了“A.炸鸡块哥哥的粉丝题”和“B.智乃想考一道鸽巢原理”等题目,用户可以通过编写代码来解决这些题目,以此锻炼自己的算法能力和编程技巧。示例代码使用了C++语言解决“A.炸鸡块哥哥的粉丝题”: ```cpp #include<bits/stdc++.h> using namespace std; int n; string s; int main() { ios::sync_with_stdio(false); cin.tie(0); cin>>n>>s; for(int i=0;i<(n+1)/2;i++) cout<<s[i]; return 0; } ``` 牛客也有算法题的在线评测系统,用户提交代码后,系统会根据预先设定的测试用例对代码进行评测,判断代码是否能正确解决问题。 在功能方面,牛客提供了丰富的算法题库,涵盖了各种难度和类型的题目,帮助用户提升编程和算法能力。它还提供不同语言的支持,例如引用中的C++和Java,方便不同语言背景的用户使用。此外,牛客还有求职相关的板块,为求职者提供企业招聘信息、面经、笔试题等,帮助他们更好地准备求职。 使用方法上,用户首先需要注册并登录牛客账号。之后可以在练习赛板块参与比赛,或者在题库中选择自己感兴趣的题目进行练习。对于算法题目,用户可以在代码编辑区域编写代码,选择合适的编程语言,然后提交代码进行评测。以解决“每日温度”问题的Java代码为例: ```java import java.util.*; /** * NC208 每日温度 * @author d3y1 */ public class Solution { /** * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可 * * 每日温度 * @param dailyTemperatures int整型一维数组 * @return int整型一维数组 */ public int[] temperatures (int[] dailyTemperatures) { int n = dailyTemperatures.length; int[] results = new int[n]; Stack<Integer> stack = new Stack<>(); // Deque<Integer> stack = new ArrayDeque<>(); // Deque<Integer> stack = new LinkedList<>(); for(int i=n-1; i>=0; i--){ // 单调栈 单调减(从右向左遍历) while(!stack.isEmpty() && dailyTemperatures[stack.peek()]<=dailyTemperatures[i]){ stack.pop(); } // if(stack.isEmpty()){ // results[i] = 0; // }else{ // results[i] = stack.peek()-i; // } results[i] = stack.isEmpty() ? 0 : stack.peek()-i; stack.push(i); } return results; } } ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值