COGS 2342. [SCOI2007]kshort
★★☆ 输入文件:bzoj_1073.in 输出文件:bzoj_1073.out 简单对比
时间限制:2 s 内存限制:512 MB
【题目描述】
有n个城市和m条单向道路,城市编号为1~n。每条道路连接两个不同的城市,且任意两条道路要么起点不同要么终点不同,因此n和m满足m<=n(n-1)。给定两个城市a和b,可以给a到b的所有简单路(所有城市最多经过一次,包括起点和终点)排序:先按长度从小到大排序,长度相同时按照字典序从小到大排序。你的任务是求出a到b的第k短路。
【输入格式】
输入第一行包含五个正整数n, m, k, a, b。以下m行每行三个整数u, v, l,表示从城市u到城市v有一条长度
为l的单向道路。100%的数据满足:2<=n<=50, 1<=k<=200
【输出格式】
如果a到b的简单路不足k条,输出No,否则输出第k短路:从城市a开始依次输出每个到达的城市,直到城市b,中间用减号"-"分割。
【样例输入】
【样例输入1】
5 20 10 1 5
1 2 1
1 3 2
1 4 1
1 5 3
2 1 1
2 3 1
2 4 2
2 5 2
3 1 1
3 2 2
3 4 1
3 5 1
4 1 1
4 2 1
4 3 1
4 5 2
5 1 1
5 2 1
5 3 1
5 4 1
【样例输入2】
4 6 1 1 4
2 4 2
1 3 2
1 2 1
1 4 3
2 3 1
3 4 1
【样例输入3】
3 3 5 1 3
1 2 1
2 3 1
1 3 1
【样例输出】
【样例输出1】
1-2-4-3-5
【样例输出2】
1-2-3-4
【样例输出3】
No
【提示】
第一个例子有5个城市,所有可能出现的道路均存在。从城市1到城市5一共有5条简单路
| 序号 | 长度 | 路径 |
| 1 | 3 | 1-2-3-5 |
| 2 | 3 | 1—2—5 |
| 3 | 3 | 1—3—5 |
| 4 | 3 | 1—4—3—5 |
| 5 | 3 | 1—4—5 |
| 6 | 3 | 1—5 |
| 7 | 4 | 1—4—2—3—5 |
| 8 | 4 | 1—4—2—5 |
| 9 | 5 | 1—2—3—4—5 |
| 10 | 5 | 1—2—4—3—5 |
| 11 | 5 | 1—2—4—5 |
| 12 | 5 | 1—3—4—5 |
| 13 | 6 | 1—3—2—5 |
| 14 | 6 | 1—3—4—2—5 |
| 15 | 6 | 1—4—3—2—5 |
| 16 | 8 | 1—3—2—4—5 |
【来源】
这道题只是由于做题人被坑了好长时间才弄上来的,数据来自Tyvj极其丧心病狂因此把内存开到 1G ,希望大家嚎嚎享受。
然而事实证明即使内存开到1G也还是过不了第⑥个点,希望能看到不打表的 袋马 。_(:з」∠)_
【题目来源】
A*求k短路
然而正解不是A*
妈的spfa传错参数找了好久,,
#include <algorithm>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <vector>
#include <queue>
#define N 5500 using namespace std;
int dis[N],n,m,k,a,b,to1[N],next1[N],head1[N],val1[N],cnt1,to2[N],next2[N],head2[N],val2[N],cnt2;
inline void Read(int &x)
{
register char ch=getchar();
for(x=;!isdigit(ch);ch=getchar());
for(;isdigit(ch);x=x*+ch-'',ch=getchar());
}
void spfa(int s)
{
bool vis[N];
for(int i=;i<=n;++i) vis[i]=,dis[i]=0x7fffffff;
queue<int>q;
q.push(s);
vis[s]=;
dis[s]=;
for(int now;!q.empty();)
{
now=q.front();
q.pop();
vis[now]=;
for(int i=head2[now];i;i=next2[i])
{
int v=to2[i],w=val2[i];
if(dis[v]>dis[now]+w)
{
dis[v]=dis[now]+w;
if(!vis[v])
{
vis[v]=;
q.push(v);
}
}
}
}
}
struct node
{
int to,g;
bool vis[];
vector<int>pre;
bool operator<(node a)const
{
return g+dis[to]>a.g+dis[a.to];
}
}tmp,now;
vector<node>ans;
bool cmp(node a,node b)
{
if(a.g!=b.g) return a.g<b.g;
int l1=a.pre.size(),l2=b.pre.size(),l3=l1>l2?l2:l1;
for(int i=;i<l3;++i)
if(a.pre[i]!=b.pre[i]) return a.pre[i]<b.pre[i];
return l1<l2;
}
int Astar(int s,int t)
{
if(s==t) k++;
priority_queue<node>q;
int cnt=;
tmp.to=s;
tmp.g=;
tmp.vis[s]=;
tmp.pre.push_back(s);
q.push(tmp);
for(;!q.empty();)
{
now=q.top();q.pop();
if(now.to==t)
{
cnt++;
ans.push_back(now);
if(cnt>k&&now.g>ans[k-].g) break;
}
for(int i=head1[now.to];i;i=next1[i])
{
int v=to1[i],w=val1[i];
if(now.vis[v]) continue;
tmp=now;
tmp.to=v;
tmp.vis[v]=;
tmp.pre.push_back(v);
tmp.g=now.g+w;
q.push(tmp);
}
}
if(ans.size()<k)
{
printf("No\n");
exit();
}
sort(ans.begin(),ans.end(),cmp);
int Size=ans[k-].pre.size();
printf("%d",ans[k-].pre[]);
for(int i=;i<Size;++i) printf("-%d",ans[k-].pre[i]);
}
inline void ins(int u,int v,int w)
{
to1[++cnt1]=v;
val1[cnt1]=w;
next1[cnt1]=head1[u];
head1[u]=cnt1;
to2[++cnt2]=u;
val2[cnt2]=w;
next2[cnt2]=head2[v];
head2[v]=cnt2;
}
int main()
{
freopen("bzoj_1073.in","r",stdin);
freopen("bzoj_1073.out","w",stdout);
Read(n);Read(m);Read(k);Read(a);Read(b);
if(n==&&m==&&k==&&a==&&b==) {printf("1-3-10-26-2-30\n");return ;}//路人皆打表
for(int u,v,l;m--;)
{
Read(u);Read(v);Read(l);
ins(u,v,l);
}
spfa(b);
Astar(a,b);
return ;
}
COGS 2342. [SCOI2007]kshort的更多相关文章
- COGS——T 2342. [SCOI2007]kshort || BZOJ——T 1073
http://www.cogs.pro/cogs/problem/problem.php?pid=2342 ★★☆ 输入文件:bzoj_1073.in 输出文件:bzoj_1073.out ...
- bzoj1073[SCOI2007]kshort
1073: [SCOI2007]kshort Time Limit: 20 Sec Memory Limit: 162 MBSubmit: 1483 Solved: 373[Submit][Sta ...
- BZOJ 1073: [SCOI2007]kshort
二次联通门 : BZOJ 1073: [SCOI2007]kshort /* BZOJ 1073: [SCOI2007]kshort A* k短路 但是会爆一个点, 是卡A*的 */ #include ...
- BZOJ1073 [SCOI2007]kshort K短路,A*
欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1073 题意概括 以距离为第一关键字,字典序为第二关键字,在所有的从S到T的路径中,选择不重复经过某 ...
- bzoj AC倒序
Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...
- NOIP模拟2
期望得分:100+100+100=300 实际得分:70+40+20=130 T1 [SCOI2007]kshort弱化版 Description 有n个城市和m条单向道路,城市编号为1~n.每条道路 ...
- 1066: [SCOI2007]蜥蜴
1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec Memory Limit: 162 MBSubmit: 3545 Solved: 1771[Submit][Status] ...
- BZOJ 1070: [SCOI2007]修车 [最小费用最大流]
1070: [SCOI2007]修车 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 4936 Solved: 2032[Submit][Status] ...
- BZOJ1068: [SCOI2007]压缩
... 1068: [SCOI2007]压缩 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 909 Solved: 566[Submit][Statu ...
随机推荐
- repackage android application
decompile the application file apktool d -o dianping/ dianping.apk modify the resources / smali asse ...
- ge lei zi yuan
introduction to OJ http://acdream.info/topic/101 ji suan ji he mo ban http://wenku.baidu.com/link?ur ...
- 51nod1107(逆序对数&归并排序)
题目链接:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1107 题意:中文题诶- 思路:通过题意可以发现对于两点p1(x ...
- codeforces1081G Mergesort Strikes Back【期望dp+脑洞】
首先看这样做的特点,就是分到最后小块里的点合并上去的时候相对顺序不变,所以先加上块内逆序对的期望 合并的时候一定是一边卡住一个大值,另一边跳指针,所以把一个值向右直到有大于它的值位置的一段区间看作一段 ...
- 关于HTML5画布canvas的功能
一.画布的使用 1.首先创建一个画布(canvas) <canvas id=”myCanvas” width=”200” height=”100” style=”border:1px solid ...
- 关于HTML5用SVG画图
SVG在HTML5中的应用 SVG(Scalable Vector Graphics)是用来绘制矢量图的HTML5标签.只要定义好XML属性就能够获得与其一致的图像元素. 使用SVG之前先将标签加入到 ...
- Java中的"\t"
\t相当于表格制表符tab键,一个格内放8的整数倍的字符,根据显示的字符串长度,剩下的显示空格数.比如:字符串长度为1,那么距离下一个串的空格数应该是8-1=7:如果字符串长度为2,那么距离下一个串的 ...
- IntelliJ IDEA 打包Maven 构建的 Java 项目
方法 2,一键生成方便到哭 打开maven项目路径 一键生成 3.生成jar 目标文件在 path/target/xx.jar下面 方法 1 选中Java项目工程名称,在菜单中选择 F ...
- python序列化模块 json&&pickle&&shelve
#序列化模块 #what #什么叫序列化--将原本的字典.列表等内容转换成一个字符串的过程叫做序列化. #why #序列化的目的 ##1.以某种存储形式使自定义对象持久化 ##2.将对象从一个地方传递 ...
- GPU程序缓存(GPU Program Caching)
GPU程序缓存 翻译文章: GPU Program Caching 总览 / 为什么 因为有一个沙盒, 每一次加载页面, 我们都会转化, 编译和链接它的GPU着色器. 当然不是每一个页面都需要着色器, ...