首页 技术 正文
技术 2022年11月18日
0 收藏 483 点赞 4,323 浏览 1704 个字

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=3760

思路:首先是建反图,从点n开始做spfa求出n到各点的最短路,然后从1点开始搜最小序列,对于边(u,v),若dist[u]==dist[v]+1,则要将当前的序号加入当前队列中,然后就是对于那些序号相同点的都要加入当前队列,还要判一下重。至于为什么要建反图从n点开始求最短路,因为在搜最小序列的时候要保证一定能搜到n点。

 #pragma comment(linker, "/STACK:1024000000,1024000000")
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
#define MAXN 1000000
#define inf 1<<30 struct Edge{
int v,w,color,next;
}edge[MAXN]; int n,m,NE;
int head[MAXN]; void Insert(int u,int v,int w,int color)
{
edge[NE].v=v;
edge[NE].w=w;
edge[NE].color=color;
edge[NE].next=head[u];
head[u]=NE++;
} bool mark[MAXN];
int dist[MAXN];
void spfa()
{
memset(mark,false,sizeof(mark));
fill(dist,dist+MAXN,inf);
dist[n]=;
queue<int>que;
que.push(n);
while(!que.empty()){
int u=que.front();
que.pop();
mark[u]=false;
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,w=edge[i].w;
if(dist[u]+w<dist[v]){
dist[v]=dist[u]+w;
if(!mark[v]){
mark[v]=true;
que.push(v);
}
}
}
}
} int vv[MAXN];
void BFS()
{
memset(mark,false,sizeof(mark));
queue<int>que;
que.push();
int flag=;
while(!que.empty()){
int num=,ans=inf;
int u=que.front();
if(u==n)break;
int size=que.size();
while(size--){
u=que.front();
que.pop();
for(int i=head[u];i!=-;i=edge[i].next){
int v=edge[i].v,w=edge[i].w,color=edge[i].color;
if(dist[u]==dist[v]+w){
if(color<ans){
ans=color;
num=;
vv[num++]=v;
}else if(color==ans){
vv[num++]=v;
}
}
}
}
if(flag){
printf("%d",ans);
flag=;
}else
printf(" %d",ans);
for(int i=;i<num;i++){
if(mark[vv[i]])continue;
mark[vv[i]]=true;
que.push(vv[i]);
}
}
puts("");
} int main()
{
int _case,u,v,color;
scanf("%d",&_case);
while(_case--){
scanf("%d%d",&n,&m);
NE=;
memset(head,-,sizeof(head));
while(m--){
scanf("%d%d%d",&u,&v,&color);
Insert(u,v,,color);
Insert(v,u,,color);
}
spfa();
printf("%d\n",dist[]);
BFS();
}
return ;
}
相关推荐
python开发_常用的python模块及安装方法
adodb:我们领导推荐的数据库连接组件bsddb3:BerkeleyDB的连接组件Cheetah-1.0:我比较喜欢这个版本的cheeta…
日期:2022-11-24 点赞:878 阅读:9,038
Educational Codeforces Round 11 C. Hard Process 二分
C. Hard Process题目连接:http://www.codeforces.com/contest/660/problem/CDes…
日期:2022-11-24 点赞:807 阅读:5,523
下载Ubuntn 17.04 内核源代码
zengkefu@server1:/usr/src$ uname -aLinux server1 4.10.0-19-generic #21…
日期:2022-11-24 点赞:569 阅读:6,371
可用Active Desktop Calendar V7.86 注册码序列号
可用Active Desktop Calendar V7.86 注册码序列号Name: www.greendown.cn Code: &nb…
日期:2022-11-24 点赞:733 阅读:6,152
Android调用系统相机、自定义相机、处理大图片
Android调用系统相机和自定义相机实例本博文主要是介绍了android上使用相机进行拍照并显示的两种方式,并且由于涉及到要把拍到的照片显…
日期:2022-11-24 点赞:512 阅读:7,785
Struts的使用
一、Struts2的获取  Struts的官方网站为:http://struts.apache.org/  下载完Struts2的jar包,…
日期:2022-11-24 点赞:671 阅读:4,867