题目描述

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.

输入

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

C++实现:

#include<iostream>
#include<vector>
using namespace std;
struct E{int next;int cost;
};
vector<E>edge[10001];
int Dis[601];
int co[601];
bool mark[601];
int main(){int n,m;while(scanf("%d",&n)!=EOF&&n!=0){for(int i=1;i<=n;i++){edge[i].clear();}cin>>m;for(int i=1;i<=m;i++){int a,b,c;cin>>a>>b>>c;E tmp;tmp.next=b;tmp.cost=c;edge[a].push_back(tmp);tmp.next=a;edge[b].push_back(tmp);}for(int i=1;i<=n;i++){cin>>co[i];}/*for(int i=1;i<n;i++){for(int j=i+1;j<=n;j++){if(co[i]!=co[j]){if(co[i]==1){edge[j].erase(edge[j].begin()+i,edge[j].begin()+i+1);}elseedge[i].erase(edge[i].begin()+j,edge[i].begin()+j+1);}}}*/for(int i=1;i<=n;i++){mark[i]=false;Dis[i]=-1;}Dis[1]=0;mark[1]=true; int newP=1;for(int i=1;i<=n;i++){for(int j=0;j<edge[newP].size();j++){int p=edge[newP][j].next;int q=edge[newP][j].cost;if(mark[p]==true)continue;//只需在此进行判断即可if(co[newP]==2&&co[p]==1)continue;if(Dis[p]==-1||Dis[p]>Dis[newP]+q){Dis[p]=Dis[newP]+q;}}int min=123123123;for(int i=1;i<=n;i++){if(mark[i]==true)continue;if(Dis[i]==-1)continue;if(min>Dis[i]){min=Dis[i];newP=i;}}mark[newP]=true;}cout<<Dis[2]<<endl;}
}

I Wanna Go Home(Dijkstra)--C++实现相关推荐

  1. 2011年北京大学计算机研究生机试真题(dijkstra+优先队列)

    http://ac.jobdu.com/problem.php?pid=1162  I Wanna Go Home 方法一:普通的dijkstra /* 很明显的最短路,但关键是如何建图.可以看到,一 ...

  2. [C] Dijkstra算法——通过边实现松弛

    Dijkstra算法--通过边实现松弛 本算法学习指定一个点(源点)到其余各个顶点的最短路径,也叫做单源最短路径例如求下图1号顶点到2,3,4,5,6号顶点的最短路径 这个时候你可能就要问了,为什么不 ...

  3. Codeforces.1051F.The Shortest Statement(最短路Dijkstra)

    题目链接 先随便建一棵树. 如果两个点(u,v)不经过非树边,它们的dis可以直接算. 如果两个点经过非树边呢?即它们一定要经过该边的两个端点,可以直接用这两个点到 u,v 的最短路更新答案. 所以枚 ...

  4. 经典算法研究系列:二、Dijkstra 算法初探

    经典算法研究系列:二.Dijkstra 算法初探  July   二零一一年一月 ====================== 本文主要参考:算法导论 第二版.维基百科. 写的不好之处,还望见谅. 本 ...

  5. 贪心算法单源点最短路径例题c语言源代码,Dijkstra算法是解单源最短路径问题的一个贪心算法...

    问题描述 给定一个带权有向图 G=(V,E) ,其中每条边的权是一个非负实数. 另外,还给定 V 中的一个项点,称为源. 现在我们要计算从源到所有其他各项点的最短路径长度. 这里的长度是指路上各边权之 ...

  6. Acwing--朴素dijkstra

    #include <iostream> #include <cstring> using namespace std;/* 优点:可以求得[n]到任一点的最短距离;可以输入最短 ...

  7. Dijkstra(迪杰斯特拉)算法简介

    目录 适用情形 思想 核心代码 设计实现更多功能 举例说明 适用情形 适用于权值为非负的图的单源最短路径 思想 在已知起点与终点的情况下.须有三个一维数组S,U,dis,S用于记录已经查找过的点,U则 ...

  8. 数据结构与算法(7-4)最短路径(迪杰斯特拉(Dijkstra)算法、弗洛伊德(Floyd)算法)

    目录 一.最短路径概念 二.迪杰斯特拉(Dijkstra)算法(单源最短路径) 1.原理 2.过程 3.代码 三.弗洛伊德(Floyd)算法(多源最短路径) 1.原理 2.存储 3.遍历 4.代码 参 ...

  9. 最短路径 - dijkstra

    dijkstra是单源点最短路算法. 借图: 其基本思想是,设置顶点集合S并不断地作贪心选择来扩充这个集合.一个顶点属于集合S当且仅当从源到该顶点的最短路径长度已知. 初始时,S中仅含有源.设u是G的 ...

最新文章

  1. Exchange企业实战技巧(16)发布SMTP、POP、IMAP连接信息设置
  2. qt 在移动的两点之间连线_几种移动端跨平台技术区别
  3. java执行时的两个常见问题(无法加载主类)
  4. HTML5 Drop API
  5. hybris impex里的多语言处理语法
  6. CF1547F Array Stabilization (GCD version) st表 + 尺取/二分
  7. RabbitMQ的5种队列_Work模式_入门试炼_第5篇
  8. linux钩子函数和回调函数,Linux Kernel 学习笔记10:hook函数
  9. sqlalchemy 聚合
  10. 2.7 HDFS的使用
  11. hibernate中uuid和native等主键生成策略
  12. ssm+爱尚购物 毕业设计-附源码211622
  13. java8 foreach 异常_在java 8流foreach中抛出异常
  14. gbk英文占几个字节
  15. Uncaught TypeError: date.getDay is not a function at getDate
  16. 彻底删除GitHub仓库的某个文件或文件夹及其历史记录
  17. axis调用java实现webservice实例
  18. OA协同办公系统 公共事务设置
  19. GoC编程(C++画图) 小学C++编程启蒙、入门、学习路线推荐
  20. 2021年 江南大学研究生考试 算法与程序设计 题目

热门文章

  1. Vue响应式原理探究之“发布-订阅”模式
  2. Cocos Creator 资源加载流程剖析【二】——Download部分
  3. 【新2023】华为OD机试 - 航天器(Python)
  4. linux学习——windows下sublinux安装出现“ubuntu不能访问网络位置+有关网络排除故障的信息”
  5. sequelize参考
  6. 控制台应用于指挥中心的技术要求是什么?
  7. JDBC:1433端口
  8. how to telnet a phone?
  9. vivado中复数乘法器IP核使用小结
  10. 第三方支付平台排行!