1073: [SCOI2007]kshort

Time Limit: 20 Sec  Memory Limit: 162 MB
Submit: 1483  Solved: 373
[Submit][Status][Discuss]

Description

  有n个城市和m条单向道路,城市编号为1~n。每条道路连接两个不同的城市,且任意两条道路要么起点不同要
么终点不同,因此n和m满足m<=n(n-1)。给定两个城市a和b,可以给a到b的所有简单路(所有城市最多经过一次,
包括起点和终点)排序:先按长度从小到大排序,长度相同时按照字典序从小到大排序。你的任务是求出a到b的第
k短路。

Input

  输入第一行包含五个正整数n, m, k, a, b。以下m行每行三个整数u, v, l,表示从城市u到城市v有一条长度
为l的单向道路。100%的数据满足:2<=n<=50, 1<=k<=200

Output

  如果a到b的简单路不足k条,输出No,否则输出第k短路:从城市a开始依次输出每个到达的城市,直到城市b,
中间用减号"-"分割。

Sample Input

【样例输入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

Sample Output

【样例输出1】
1-2-4-3-5
【样例输出2】
1-2-3-4
【样例输出3】
No

HINT

第一个例子有5个城市,所有可能出现的道路均存在。从城市1到城市5一共有5条简单路

调了很久,发现竟然是spfa手动队列数组开小了,,,
A*算法求K短路吧,状压判重
听说过不了,要加特判
听说有更强的YEN算法,懒得学。

 #include<bits/stdc++.h>
#define ll long long
#define N 55
using namespace std;
int n,m,K,s,t,cnt,tot,ent,qe[N*],d[N],hd[N],HD[N],vis[N];
struct edge{int v,w,next;}e[N*N],E[N*N];
struct pth{
int pre,dis,ls;ll vis;vector<int>c;
pth(){dis=pre=;vis=;c.clear();}
bool operator < (const pth &b)const{
if(dis!=b.dis)return dis>b.dis;
int len=min(c.size(),b.c.size());
for(int i=;i<len;i++)
if(c[i]!=b.c[i])return c[i]>b.c[i];
return c.size()>b.c.size();
}
};
void adde(int u,int v,int w){
e[++tot].v=v;
e[tot].w=w;
e[tot].next=hd[u];
hd[u]=tot;
}
void ADDE(int u,int v,int w){
E[++ent].v=v;
E[ent].w=w;
E[ent].next=HD[u];
HD[u]=ent;
}
void spfa(){
memset(d,0x3f,sizeof(d));
int l=,r=;qe[++r]=t;d[t]=;
while(l<=r){
int u=qe[l++];vis[u]=;
for(int i=HD[u];i;i=E[i].next){
int v=E[i].v;
if(d[v]>d[u]+E[i].w){
d[v]=d[u]+E[i].w;
if(vis[v])continue;
vis[v]=;qe[++r]=v;
}
}
}
}
priority_queue<pth>q;
void Astar(){
pth tmp;tmp.vis|=1ll<<(s-);
tmp.c.push_back(s);tmp.ls=s;
q.push(tmp);
while(!q.empty()){
if (q.size()>)break;
pth u=q.top();q.pop();
if(u.ls==t)cnt++;
if(cnt==K){
for(int i=;i<u.c.size();i++){
int x=u.c[i];printf("%d",x);
if(x!=t)putchar('-');
else putchar('\n');
}
break;
}
if(u.ls==t)continue;
for(int i=hd[u.ls];i;i=e[i].next){
int v=e[i].v;
if(u.vis&(1ll<<(v-)))continue;
tmp=u;tmp.ls=v;tmp.pre+=e[i].w;
tmp.dis=tmp.pre+d[v];tmp.vis|=1ll<<(v-);
tmp.c.push_back(v);q.push(tmp);
}
}
}
int main(){
scanf("%d%d%d%d%d",&n,&m,&K,&s,&t);
if(m==){
printf("1-3-10-26-2-30\n");
return ;
}
for(int i=;i<=m;i++){
static int u,v,w;
scanf("%d%d%d",&u,&v,&w);
adde(u,v,w);ADDE(v,u,w);
}
spfa();Astar();
if(cnt<K)puts("No");
return ;
}

bzoj1073[SCOI2007]kshort的更多相关文章

  1. BZOJ1073 [SCOI2007]kshort K短路,A*

    欢迎访问~原文出处——博客园-zhouzhendong 去博客园看该题解 题目传送门 - BZOJ1073 题意概括 以距离为第一关键字,字典序为第二关键字,在所有的从S到T的路径中,选择不重复经过某 ...

  2. BZOJ 1073: [SCOI2007]kshort

    二次联通门 : BZOJ 1073: [SCOI2007]kshort /* BZOJ 1073: [SCOI2007]kshort A* k短路 但是会爆一个点, 是卡A*的 */ #include ...

  3. COGS 2342. [SCOI2007]kshort

    ★★☆   输入文件:bzoj_1073.in   输出文件:bzoj_1073.out   简单对比时间限制:2 s   内存限制:512 MB [题目描述] 有n个城市和m条单向道路,城市编号为1 ...

  4. COGS——T 2342. [SCOI2007]kshort || BZOJ——T 1073

    http://www.cogs.pro/cogs/problem/problem.php?pid=2342 ★★☆   输入文件:bzoj_1073.in   输出文件:bzoj_1073.out   ...

  5. NOIP模拟2

    期望得分:100+100+100=300 实际得分:70+40+20=130 T1 [SCOI2007]kshort弱化版 Description 有n个城市和m条单向道路,城市编号为1~n.每条道路 ...

  6. bzoj AC倒序

    Search GO 说明:输入题号直接进入相应题目,如需搜索含数字的题目,请在关键词前加单引号 Problem ID Title Source AC Submit Y 1000 A+B Problem ...

  7. 1066: [SCOI2007]蜥蜴

    1066: [SCOI2007]蜥蜴 Time Limit: 1 Sec  Memory Limit: 162 MBSubmit: 3545  Solved: 1771[Submit][Status] ...

  8. BZOJ 1070: [SCOI2007]修车 [最小费用最大流]

    1070: [SCOI2007]修车 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 4936  Solved: 2032[Submit][Status] ...

  9. BZOJ1068: [SCOI2007]压缩

    ... 1068: [SCOI2007]压缩 Time Limit: 1 Sec  Memory Limit: 128 MBSubmit: 909  Solved: 566[Submit][Statu ...

随机推荐

  1. 在linux中关闭防火墙

    1) 重启后生效 开启: chkconfig iptables on 关闭: chkconfig iptables off 2) 即时生效,重启后失效 开启: service iptables sta ...

  2. DES MEI号码加密

    对于加密来说,使用DES加密解密很好,但是为了使不同设备的密文不一样,可以使用 固定字符串 + 设备IMEI号码 加密的方式,这样可以分辨不同手机,限制手机的使用(若是注册,一个手机只有一个IMEI号 ...

  3. Linux下关闭Tomcat残留线程

    ps -ef | grep tomcat kill -9 {pid}

  4. 03-移动端开发教程-CSS3新特性(下)

    1. CSS3动画 1.1 过渡的缺点 transition的优点在于简单易用,但是它有几个很大的局限. transition需要事件触发,所以没法在网页加载时自动发生. transition是一次性 ...

  5. sql 多条记录插入

    --多条记录插入,用逗号分开值. INSERT dbo.studentinfor ( id, name, class, age, hpsw ) ', -- id - nvarchar(50) N'te ...

  6. Python-socket网络编程-Day8

    目录Day8-Python socket 11.Socket 11.1.socket和file的区别: 11.2.WEB服务应用: 21.3.更多功能 21.4.socket方法: 41.5. 服务端 ...

  7. 请求方式:request和 get、post、put

    angular 的 http 多了 Request, Headers, Response ,这些都是游览器的"新特性" Fetch API. Fetch API 和以前的 xmlh ...

  8. restful架构风格设计准则(一)以资源为中心、自描述的请求响应、资源状态迁移为粒度

    读书笔记,原文链接:http://www.cnblogs.com/loveis715/p/4669091.html,感谢作者! 一.需求描述 当用户在某个电子商务网站购物时,他首先查看要购买的商品分类 ...

  9. Python之面向对象三

    面向对象的三大特性: 多态 多态指的是一类事物有多种形态.Python3天生支持多态. 动物有多种形态:人,狗,猪 import abc class Animal(metaclass=abc.ABCM ...

  10. Spring MVC基础学习

    SpringMVC是Spring框架的一个模块,无需通过中间层整合在一起.SpringMVC是一个基于MVC设计模式web框架,MVC-model-view-controller:MVC将服务器端分为 ...