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 ...
随机推荐
- poj 2195 Going Home (km算法)
题目链接: http://poj.org/problem?id=2195 解题思路: 把man和home都提取出来,然后算出每个man和home的距离算出来,然后建立匹配图,套用km算法的模板,求最小 ...
- 题解报告:hdu 1176 免费馅饼(递推dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1176 Problem Description 都说天上不会掉馅饼,但有一天gameboy正走在回家的小 ...
- 基于CentOS6.5下Suricata(一款高性能的网络IDS、IPS和网络安全监控引擎)的搭建(图文详解)(博主推荐)
不多说,直接上干货! 为什么,要写这篇论文? 是因为,目前科研的我,正值研三,致力于网络安全.大数据.机器学习研究领域! 论文方向的需要,同时不局限于真实物理环境机器实验室的攻防环境.也不局限于真实物 ...
- HashMap和List遍历方法总结及如何遍历删除元素
https://blog.csdn.net/demohui/article/details/77748809
- Android 使用pl.droidsonroids.gif.GifImageView在安卓中显示动图遇到的问题
在做一款聊天软件,其中聊天界面需要发送表情,而表情都是动图,在安卓中想要显示动图,就要借助第三方框架,我选的是pl.droidsonroids.gif.GifImageView. 使用方法如下:你在g ...
- R Programming week2 Control Structures
Control Structures Control structures in R allow you to control the flow of execution of the program ...
- fatal: Authentication failed for 问题解决
执行以下code git config --system --unset credential.helper 参考地址: https://www.jianshu.com/p/8a7f257e07b8
- 数组(Arry)几个常用方法的详解
join() 方法用于把数组中的所有元素放入一个字符串.元素是通过指定的分隔符进行分隔的. arrayObject.join(separator)separator 可选.指定要使用的分隔符.如果省略 ...
- (一)Redis for Windows正确打开方式
目录 (一)Redis for Windows正确打开方式 (二)Redis for 阿里云公网连接 (三)Redis for StackExchange.Redis 下载地址 官网.中文网1 及 中 ...
- UEFI启动 安装win8 win10 及windows server 2012 最简单的方法
纯UEFI模式只认U盘 纯UEFI模式下U盘安装的具体步骤其实很简单: 1.BIOS设置中启动项关闭兼容模式 ...