UVA-11374(最短路)
题意:
机场快线有经济线和商业线,现在分别给出经济线和商业线的的路线,现在只能坐一站商业线,其他坐经济线,问从起点到终点的最短用时是多少,还有路线是怎样的;
思路:
预处理出起点到所有站的最短距离和终点到所有站的最短距离,枚举要坐的那趟商业线,然后里面最小的就是答案了;
AC代码:
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <bits/stdc++.h>
#include <stack> using namespace std; #define For(i,j,n) for(int i=j;i<=n;i++)
#define mst(ss,b) memset(ss,b,sizeof(ss)); typedef long long LL; template<class T> void read(T&num) {
char CH; bool F=false;
for(CH=getchar();CH<'0'||CH>'9';F= CH=='-',CH=getchar());
for(num=0;CH>='0'&&CH<='9';num=num*10+CH-'0',CH=getchar());
F && (num=-num);
}
int stk[70], tp;
template<class T> inline void print(T p) {
if(!p) { puts("0"); return; }
while(p) stk[++ tp] = p%10, p/=10;
while(tp) putchar(stk[tp--] + '0');
putchar('\n');
} const LL mod=1e9+7;
const double PI=acos(-1.0);
const int inf=1e9;
const int N=2e5+10;
const int maxn=500+10;
const double eps=1e-6; int n,s,e,m;
int mp[maxn][maxn],dis[2][maxn],vis[maxn],p[2][maxn]; inline void Dijkstra(int from,int flag)
{
mst(vis,0);
vis[from]=1;
For(i,1,n)dis[flag][i]=mp[from][i],p[flag][i]=from;
dis[flag][from]=0;
For(i,2,n)
{
int temp=inf,k;
For(j,1,n)
if(!vis[j]&&temp>dis[flag][j])k=j,temp=dis[flag][j];
if(temp==inf)break;
vis[k]=1;
For(j,1,n)
{
if(!vis[j]&&dis[flag][j]>dis[flag][k]+mp[k][j])
dis[flag][j]=dis[flag][k]+mp[k][j],p[flag][j]=k;
}
}
} int main()
{
int Case=0;
while(cin>>n>>s>>e)
{
if(Case)printf("\n");
Case++;
For(i,1,n)For(j,1,n)mp[i][j]=inf;
read(m);
int u,v,w;
For(i,1,m)
{
read(u);read(v);read(w);
mp[u][v]=mp[v][u]=min(mp[u][v],w);
}
Dijkstra(s,0);
Dijkstra(e,1);
int ans=dis[0][e],uppos=-1,downpos=-1;
int q;
read(q);
For(i,1,q)
{
read(u);read(v);read(w);
int temp=dis[0][u]+w+dis[1][v];
// cout<<dis[0][u]<<" "<<dis[1][v]<<endl;
if(temp<ans)ans=temp,uppos=u,downpos=v;
temp=dis[0][v]+w+dis[1][u];
if(temp<ans)ans=temp,uppos=v,downpos=u;
}
if(uppos==-1)
{
int cur=s;
while(cur!=e)printf("%d ",cur),cur=p[1][cur];
printf("%d\nTicket Not Used\n%d\n",e,ans);
}
else
{
int cur=uppos,cnt=0,tion[maxn];
while(cur!=s)
{
tion[++cnt]=cur;
cur=p[0][cur];
}
tion[++cnt]=s;
for(int i=cnt;i>0;i--)printf("%d ",tion[i]);
cur=downpos;
while(cur!=e)printf("%d ",cur),cur=p[1][cur];
printf("%d\n%d\n%d\n",e,uppos,ans);
}
//printf("\n");
}
return 0;
}
UVA-11374(最短路)的更多相关文章
- uva 11374 最短路+记录路径 dijkstra最短路模板
UVA - 11374 Airport Express Time Limit:1000MS Memory Limit:Unknown 64bit IO Format:%lld & %l ...
- 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板)
layout: post title: 训练指南 UVA - 11374(最短路Dijkstra + 记录路径 + 模板) author: "luowentaoaa" catalo ...
- UVA - 11374 - Airport Express(堆优化Dijkstra)
Problem UVA - 11374 - Airport Express Time Limit: 1000 mSec Problem Description In a small city c ...
- UVA 11374 Airport Express(最短路)
最短路. 把题目抽象一下:已知一张图,边上的权值表示长度.现在又有一些边,只能从其中选一条加入原图,使起点->终点的距离最小. 当加上一条边a->b,如果这条边更新了最短路,那么起点st- ...
- UVA 11374 Halum (差分约束系统,最短路)
题意:给定一个带权有向图,每次你可以选择一个结点v 和整数d ,把所有以v为终点的边权值减少d,把所有以v为起点的边权值增加d,最后要让所有的边权值为正,且尽量大.若无解,输出结果.若可无限大,输出结 ...
- UVA 11374 Airport Express 机场快线(单源最短路,dijkstra,变形)
题意: 给一幅图,要从s点要到e点,图中有两种无向边分别在两个集合中,第一个集合是可以无限次使用的,第二个集合中的边只能挑1条.问如何使距离最短?输出路径,用了第二个集合中的哪条边,最短距离. 思路: ...
- UVA 11374 Airport Express(枚举+最短路)
枚举每条商业线<a, b>,设d[i]为起始点到每点的最短路,g[i]为终点到每点的最短路,ans便是min{d[a] + t[a, b] + g[b]}.注意下判断是否需要经过商业线.输 ...
- UVA 11374 Airport Express (最短路)
题目只有一条路径会发生改变. 常见的思路,预处理出S和T的两个单源最短路,然后枚举商业线,商业线两端一定是选择到s和t的最短路. 路径输出可以在求最短路的同时保存pa数组得到一棵最短路树,也可以用di ...
- uva 11374
Problem D: Airport Express In a small city called Iokh, a train service, Airport-Express, takes resi ...
- uva 10269 最短路
求两次最短路 #include <cstdio> #include <cstdlib> #include <cmath> #include <map> ...
随机推荐
- Unable to lock the administration directory (/var/lib/dpkg/) is another process using it?
I see pretty much all the answers recommend deleting the lock. I don't recommend doing that as a fir ...
- phonegap工程搭建基础(一)
官网:http://cordova.apache.org 一.环境配置 1. 安装Cordova on OS X and Linux: $ sudo npm install -g cord ...
- Android 组件之Service解析
原创文章,转载请注明 http://blog.csdn.net/leejizhou/article/details/50866875 李济洲的博客 Service是Android四大组件之中的一个.S ...
- phpcms v9中 action="position" 和action="lists"有什么差别, 以及action 的属性和值
action值的含义: lists 内容数据(文章?)列表 relation 内容相关文章 hits 内容数据点击排行榜 category 内容栏目列表 position 内容推荐位列表
- 谈谈Runtime类中的freeMemory,totalMemory,maxMemory几个方法
最近在网上看到一些人讨论到java.lang.Runtime类中的freeMemory(),totalMemory(),maxMemory ()这几个方法的一些问题,很多人感到很疑惑,为什么,在jav ...
- ftp的实现
ftp.h #define BUFSIZE 512#define CMDSIZE 64#define ARGSIZE 64#define PASSIVE_ON 0x1 struct ftpcmd{ c ...
- Linux基础(1)- 命令和目录文件
1.开启Linux操作系统,要求以root用户登录GNOME图形界面,语言支持选择为汉语 Linux操作界面如图: 右击桌面,点击打开终端 输入“su”,点击回车键,出现密码,输入密码,点击回车键,显 ...
- Codeforces Round #267 (Div. 2) B. Fedor and New Game
After you had helped George and Alex to move in the dorm, they went to help their friend Fedor play ...
- Android性能优化之中的一个 布局优化
本文为Android性能优化--布局优化,主要介绍使用抽象布局标签(include, viewstub, merge).去除不必要的嵌套和View节点.降低不必要的infalte及其它Layout方面 ...
- Markdown 语法的超快速上手
本文支持WTFPL协议,因此你想往哪转就往哪转. Why markdown? Markdown是一种可以使用普通文本编辑器编写的标记语言,通过简单的标记语法,它可以使普通文本内容具有一定的格式. Ma ...