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 ...
随机推荐
- jmeter(三)参数传递
[一]参数化 录制脚本中有登录操作,需要输入用户名和密码,假如系统不允许相同的用户名和密码同时登录,或者想更好的模拟多个用户来登录系统. 这个时候就需要对用户名和密码进行参数化,使每个虚拟用户都使用不 ...
- 423 Reconstruct Original Digits from English 从英文中重建数字
给定一个非空字符串,其中包含字母顺序打乱的英文单词表示的数字0-9.按升序输出原始的数字.注意: 输入只包含小写英文字母. 输入保证合法并可以转换为原始的数字,这意味着像 "ab ...
- 223 Rectangle Area 矩形面积
在二维平面上计算出两个由直线构成的矩形叠加覆盖后的面积. 假设面积不会超出int的范围. 详见:https://leetcode.com/problems/rectangle-area/descrip ...
- Windows 7下如何在Cygwin下正确安装Tcpreplay(图文详解)
可以在大家安装的Cygwin的安装目录下执行(我的这里是D:\SoftWare\cygwin) #winpcap的安装过程:|$ unzip WpdPack_4_1_2.zip|$ cp -r Wpd ...
- Android Studio 导入新工程项目
1 导入之前先修改工程下相关文件 1.1 只需修改如下三个地方1.2 修改build.gradle文件 1.3 修改gradle/wrapper/gradle-wrapper.properties 1 ...
- MTK处理器手机 解锁Bootloader 教程
目前很多手机都需要解锁Bootloader之后才能进行刷机操作 本篇教程教你如何傻瓜式解锁Bootloader 首先需要在设置-关于手机 找到版本号(个别手机可能是内核版本号,甚至其他) 然后 快 ...
- iOS Programming View and View Hierarchy 视图和视图等级
iOS Programming View and View Hierarchy 视图和视图等级 1.1(1)File → New → Project.. From the iOS section, ...
- was--创建概要文件(典型)
1.第一步 2 .创建 3.创建 4 .典型 5 下一步 6 下一步 7.下一步 8.输入用户和密码,下一步 9.下一步 10.下一步 11.下一步 12.下一步 13.下一步 14.创建 ...
- ERROR 1 (HY000): Can't create/write to file '/tmp/#sql_830_0.MYI' (Errcode: 13)
mysql操作时,出现报错. 执行describe 命令时, 临时文件目录没有创建或者无写入权限:于是: cd /var/lib/mysql/ #进入mysql数据目录 mkdir tmp #创建需要 ...
- Python+CGI,在Windows上快速部署Python到IIS
通过CGI,我们可以快速在Windows上部署Python 1. Windows安装IIS服务 2. 在IIS里打开“ISAPI和CGI限制”->添加,路径=python.exe的完全路径+&q ...