bzoj1073[SCOI2007]kshort

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条简单路

bzoj1073[SCOI2007]kshort

调了很久,发现竟然是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 ;
}
上一篇:Fiddler抓包使用教程-Https


下一篇:事件:target与currentTarget区别