xtu Shortest Path
Acceteped : 23 | Submit : 61 | |
Time Limit : 5000 MS | Memory Limit : 65536 KB |
Description |
||
题目描述N(3≤N≤1,000)个城市(编号从1~N),M(N-1≤M≤10,000)条公路连接这些城市,每条公路都是双向通车的。 你想从1号城市出发,到达N号城市,期间你希望通过按顺序经过K(0≤K≤3)个指定的城市(这K+2个城市只允许达到1次),求最短的里程。 输入存在多个样例。 每个样例的第一行是三个整数N,M,K。如果N,M,K为0,则表示输入结束。 以后是M行表示M条公路,每行三个整数x(1≤x≤N),y(1≤y≤N),c(1≤c≤1,000),表示城市x与城市y之间有一条距离为c的公路。输入保证任意两座城市之间至少存在一条路。然后的一行包含K个城市的序号,序号属于[2,N-1]。 输出每行输出一个样例的结果,为一个整数。如果不存在这样的方案,输出“Impossible”。 样例输入
样例输出
|
||
Sample Input |
||
Sample Output |
||
Source |
解题:题目说了,只限起点终点,以及要求的几个点只能访问一次,其他点嘛,呵呵!选择微软的C++编译器,速度快,g++慢得不行
- #include <iostream>
- #include <cstdio>
- #include <cstring>
- #include <cmath>
- #include <algorithm>
- #include <climits>
- #include <vector>
- #include <queue>
- #include <cstdlib>
- #include <string>
- #include <set>
- #include <stack>
- #define LL long long
- #define pii pair<int,int>
- #define INF 0x3f3f3f3f
- using namespace std;
- const int maxn = ;
- struct arc {
- int to,next;
- };
- int head[maxn],mp[maxn][maxn],tot;
- int n,m,k,city[],d[maxn];
- bool done[maxn];
- arc g[maxn<<];
- priority_queue< pii,vector< pii >,greater< pii > >q;
- void add(int u,int v) {
- g[tot].to = v;
- g[tot].next = head[u];
- head[u] = tot++;
- }
- void dijkstra(int s,int t) {
- int i,u,v;
- for(i = ; i <= n; i++) {
- d[i] = INF;
- done[i] = false;
- }
- for(i = ; i <= k+; i++)
- if(city[i] != s && city[i] != t)
- done[city[i]] = true;
- while(!q.empty()) q.pop();
- d[s] = ;
- q.push(make_pair(d[s],s));
- while(!q.empty()) {
- u = q.top().second;
- q.pop();
- if(done[u]) continue;
- done[u] = true;
- for(i = head[u]; i != -; i = g[i].next) {
- if(d[g[i].to] > d[u]+mp[u][g[i].to]) {
- d[g[i].to] = d[u]+mp[u][g[i].to];
- q.push(make_pair(d[g[i].to],g[i].to));
- }
- }
- if(done[t]) return;
- }
- }
- int main() {
- int i,j,u,v,w,sum;
- while(scanf("%d %d %d",&n,&m,&k),n+m+k) {
- for(i = ; i <= n; i++) {
- head[i] = -;
- for(j = ; j <= n; j++)
- mp[i][j] = INF;
- }
- for(tot = i = ; i < m; i++) {
- scanf("%d %d %d",&u,&v,&w);
- if(mp[u][v] == INF) {
- mp[u][v] = mp[v][u] = w;
- add(u,v);
- add(v,u);
- } else if(mp[u][v] > w) {
- mp[u][v] = mp[v][u] = w;
- }
- }
- city[] = ;
- for(i = ; i <= k; i++) scanf("%d",city+i);
- city[i] = n;
- bool flag = true;
- for(sum = i = ; i <= k; i++) {
- dijkstra(city[i],city[i+]);
- if(d[city[i+]] == INF) {flag = false;break;}
- sum += d[city[i+]];
- }
- flag?printf("%d\n",sum):puts("Impossible");
- }
- return ;
- }
xtu Shortest Path的更多相关文章
- hdu-----(2807)The Shortest Path(矩阵+Floyd)
The Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others ...
- zoj 2760 How Many Shortest Path 最大流
题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=1760 Given a weighted directed graph ...
- The Shortest Path in Nya Graph
Problem Description This is a very easy problem, your task is just calculate el camino mas corto en ...
- hdu 3631 Shortest Path(Floyd)
题目链接:pid=3631" style="font-size:18px">http://acm.hdu.edu.cn/showproblem.php?pid=36 ...
- Shortest Path(思维,dfs)
Shortest Path Accepts: 40 Submissions: 610 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: ...
- Shortest Path
Shortest Path Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)T ...
- (中等) HDU 4725 The Shortest Path in Nya Graph,Dijkstra+加点。
Description This is a very easy problem, your task is just calculate el camino mas corto en un grafi ...
- 【ZOJ2760】How Many Shortest Path
How Many Shortest Path 标签: 网络流 描述 Given a weighted directed graph, we define the shortest path as th ...
- [Swift]LeetCode847. 访问所有节点的最短路径 | Shortest Path Visiting All Nodes
An undirected, connected graph of N nodes (labeled 0, 1, 2, ..., N-1) is given as graph. graph.lengt ...
随机推荐
- 二分查找+数学 HDOJ 4342 History repeat itself
题目传送门 题意:计算从1开始到第n个非完全平方数的开方和 分析:设第n个非完全平方数的值为a,x * x < a < (x+1) * (x+1),而且易得(tmp = sqrt (a) ...
- VS2010下安装Opencv 分类: Opencv 2014-11-02 13:51 778人阅读 评论(0) 收藏
Opencv作为一种跨平台计算机视觉库,在图像处理领域得到广泛的应用,下面介绍如何在VS2010中安装配置Opencv 一.下载Opencv 下载网址:http://sourceforge.net/p ...
- java getDocumentBase() 得到的文件夹路径
参考一个百度知道上的回答 举例说来,假设你的项目文件是xx,而这个xx文件夹是在D盘下的yy文件夹里,即项目文件的完整路径D:\yy\xx,则编译运行文件后,在xx文件夹里会产生名为build的文件夹 ...
- windwsform登录页面
简单登录设计: 读取用户名密码 数据库表 实体类 数据访问类: 隐藏登录页面: 回车快捷键: 传值到main窗口:
- 一个简单的Java代码生成工具—根据数据源自动生成bean、dao、mapper.xml、service、serviceImpl
目录结构 核心思想 通过properties文件获取数据源—>获取数据表的字段名称.字段类型等—>生成相应的bean实体类(po.model).dao接口(基本的增删改查).mapper. ...
- 学习笔记 第十三章 使用CSS3新布局
第13章 使用CSS3新布局 [学习重点] 设计多列布局 设计弹性盒布局样式 使用CSS3布局技术设计适用移动需求的网页 13.1 多列布局 CSS3使用columns属性定义多列布局,用法如下 ...
- springmvc 的配置 annotation-config/annotation-drive/ component-scan 区别
1. <context:annotation-config /> 作用隐式的配置注解的加载类,默认的加载了AutowiredAnnotationBeanPostProcessor(auto ...
- scala基础篇-03 if与for
一.Scala中的if是表达式** 1.定义方式 2.例子 二.for 的用法 1.定义方式: for{ x <- xs y=x+ ) }yield y 2.例子:
- zookeeper启动
Zookeeper启动总结1.实际项目用的是Linux,问题不大,本地开发学习用Windows,问题多多.2.Zookeeper3.5.1-alpha,和本地JDK1.7,有冲突,无法正常启动.3.Z ...
- adobe开发软件激活
稳定支持至2017版本系列的adobe开发软件破解激活 本内容属原创,转载请注明出处! 以激活AE CC2017为例演示: 第一步打开软件第二步在产品列表中选择你所安装的产品(注意区分 32 位和 ...