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 
1 2 3
2 3 4
1 3 2
2
0 0 0

样例输出

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 ;
}

最新文章

  1. 山东省第七届ACM省赛------Memory Leak
  2. ASP.net 上传
  3. 一个用C++写的Json解析与处理库
  4. linux基础命令大全
  5. Git学习 -- 冲突解决
  6. 封装及propery的使用
  7. Kafka生产者-向Kafka中写入数据
  8. 利用clonezilla克隆、还原CentOS整个系统
  9. ls 命令
  10. PAT 乙级 1092 最好吃的月饼 (20 分)
  11. 【Unity】2.5 场景视图(Scene)
  12. cocos2dx ScrollView的用法
  13. 160712、Dubbo与Zookeeper、SpringMVC整合和使用(负载均衡、容错)
  14. C++中的Trivial 、POD、non-POD和Standard Layout概念
  15. vijos 1250 最勇敢的机器人 分组背包+并查集
  16. React方法论
  17. VPS Linux SSH 客户端断开后保持进程继续运行配置方法——screen
  18. CodeForces 438D 线段树 剪枝
  19. 图形化unix/linux 工具 mobarxterm
  20. Flume基本概念

热门文章

  1. ACM_查找ACM(加强版)
  2. UWP Windows10开发更新磁贴和动态更新磁贴
  3. jquery各种选择器示例
  4. (四)SpringIoc之Bean装配
  5. VUE学习,is 特性,转载来源:https://segmentfault.com/q/1010000007205176/
  6. 来自锐动天地的直播ios SDK
  7. Vuex/Vue 练手项目 在线汇率转换器
  8. 洛谷 P1364 医院设置
  9. vue动态设置页面title方法
  10. Python之__class__.__module__,__class__.__name__