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 ...
随机推荐
- 《白书》上线段树RMQ的实现
白书上的线段树RMQ实现,自己重写了一遍: #include <bits/stdc++.h> using namespace std; const int MAXN=1<<17 ...
- 找规律/贪心 Codeforces Round #310 (Div. 2) A. Case of the Zeros and Ones
题目传送门 /* 找规律/贪心:ans = n - 01匹配的总数,水 */ #include <cstdio> #include <iostream> #include &l ...
- 实现字符串的查找和替换 分类: c/c++ 2014-10-09 22:33 469人阅读 评论(0) 收藏
在字符串中查找目标字符串并将其替换为指定字符串,返回替换的次数.接口为 int find_str_replace(char *&str,const char *find_str,const c ...
- BufferedStream
处理流,包在别的流上面的流,相当于包到别的管道上面的管道. 缓冲刘: 缓冲流,就是带小桶的带缓冲区的. 那两个方法知道一下就好了,不必深究…… bis.read() read一下读一个出来 bi ...
- 不重启IIS修改dotnet framework版本
因为公司现在存在.net站点和asp站点共同运行的情况,所以需要对IIS进行一些修改,运行环境Win2003+IIS6 一.起因 原来的老站是asp开发的,用的是.net 2.0运行环境; 新站是.n ...
- Swiper插件轮播
<html><head> <meta charset="utf-8"> <title>Swiper轮播</title>& ...
- ES之值类型以及堆和栈
ES的数据类型: 原始类型(值存在栈内存中): Number.String Boolean.undefined.null charAt(index)返回该index所在的字节,charCodeAt(i ...
- updating error reports database解决方案
Window--->Preferences--->General--->Startup and Shutdown--->取消勾选Eclipse Automated Error ...
- group by 和 聚合函数的使用
有这样一个表数据: 学生姓名,学生手机号,上课日期,上课科目 科目分: 语文.数学.英语.计算机 要求统计一个这样子的结果: 学生姓名,学生手机号,第一次上课日期,迄今一共上了多少节课,上的最多的科目 ...
- eureka 注册中心
1.eureka版本更新后,pom依赖名称变化 v1.2.7spring-cloud-starter-eureka-server v2.0.0spring-cloud-starter-netflix- ...