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”。 样例输入3 3 1 样例输出7 |
||
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 ...
随机推荐
- [SHOI2002]取石子游戏之三
Wythoff's Game,详解请见浅谈算法--博弈论中的例6 /*program from Wolfycz*/ #include<cmath> #include<cstdio&g ...
- 转】upstart封装mongodb应用为系统服务
原博文出自于: http://blog.fens.me/category/%E6%95%B0%E6%8D%AE%E5%BA%93/page/4/ 感谢! upstart封装mongodb应用为系统服务 ...
- 原型模式及php实现
原型模式: 通过复制已经存在的实例来返回新的实例,而不是新建实例,并且原型(被复制的实例)是可定制的:原型模式多用于创建复杂的或耗时的实例,这种情况下,复制一个已经存在的实例是程序运行更高效无疑是一种 ...
- 一种结合hudson的算法自动化测试构想
作者:朱金灿 来源:http://blog.csdn.net/clever101 有时我在思考:未来软件测试的趋势是什么?其实答案和其它行业一样简单:低技术含量的测试工作都将由机器承担,人只能干机器干 ...
- git --删除文件、重命名
修改最后一次提交 git commit --amend -m “” 删除文件:. git rm <需要删除的文件> 只是删除当前工作目录和暂存区的文件,也就是取消跟踪.在下次提交时不纳入版 ...
- 架构(Architecture)随想
架构(Architecture)的意义: 先不要看什么是架构,先看下architect是什么,没有错,它是建筑师,在一块空地上build高楼大厦的人,它是一个designer,设计好整个大楼,也是一个 ...
- raw cannot be resolved or is not a field解决办法
解决raw文件夹问题 查看左侧项目/res文件夹下是否有raw文件夹,(一定是放到res文件夹下,raw在项目开始创建时候不会自动创建,所以要自己创建)
- 浅谈CSS中的定位知识
1,静态定位(static) 表示按照正常定位方案,元素盒按照在文档流中出现的顺序依次格式化: 2,相对定位(relative) 将移动元素盒,但是它在文档流中的原始空间会保留下来: 相对定位元素有如 ...
- chatops--rocketchat+hubot
chatops--rocketchat+hubot 原文地址:http://www.cnblogs.com/caoguo/p/7221956.html 先放几张图 # rocket.chat # hu ...
- Android(java)学习笔记195:ContentProvider使用之添加数据到联系人(掌握)
1.添加联系人逻辑思路 (1)首先在raw_contacts创建一个新的id (2)在data表里面添加这个id对应的数据 2.下面通过一个案例,说明一下如何添加一条数据到联系人: (1)首先我们关注 ...