UVA - 1599 Ideal Path (一遍树上BFS)

 UVA - 1599  Ideal Path 

New labyrinth attraction is open in New Lostland amusement park. The labyrinth consists of n rooms connected by m passages. Each passage is colored into some color ci. Visitors of the labyrinth are dropped from the helicopter to the room number 1 and their goal is to get to the labyrinth exit located in the room number n.

Labyrinth owners are planning to run a contest tomorrow. Several runners will be dropped to the room number 1. They will run to the room number n writing down colors of passages as they run through them. The contestant with the shortest sequence of colors is the winner of the contest. If there are several contestants with the same sequence length, the one with the ideal path is the winner. The path is the ideal path if its color sequence is the lexicographically smallest among shortest paths.

Andrew is preparing for the contest. He took a helicopter tour above New Lostland and made a picture of the labyrinth. Your task is to help him find the ideal path from the room number 1 to the room number n that would allow him to win the contest.

 


Note:

A sequence (a1, a2,..., ak) is lexicographically smaller than a sequence (b1, b2,..., bk) if there exists i such that ai < bi, and aj = bj for all ji.

 

Input 

The input file contains several test cases, each of them as described below.

The first line of the input file contains integers n and m -- the number of rooms and passages, respectively (2$ \le$n$ \le$100000, 1$ \le$m$ \le$200000). The following m lines describe passages, each passage is described with three integer numbers: ai, bi, and ci -- the numbers of rooms it connects and its color (1$ \le$ai, bi$ \le$n, 1$ \le$ci$ \le$109). Each passage can be passed in either direction. Two rooms can be connected with more than one passage, there can be a passage from a room to itself. It is guaranteed that it is possible to reach the room number nfrom the room number 1.

 

Output 

For each test case, the output must follow the description below.

The first line of the output file must contain k -- the length of the shortest path from the room number 1 to the room number n. The second line must contain k numbers -- the colors of passages in the order they must be passed in the ideal path.

 

Sample Input 

 

4 6 
1 2 1
1 3 2
3 4 3
2 3 1
2 4 4
3 1 1

 

Sample Output 

 

2 
1 3

白书上的原题,题意是求出最短路中颜色字典序也最小的那条路径,白书上的意思是先进行一次逆向DFS把节点的深度跑出来再正向DFS总去走那个深度深一格节点的颜色小的路。但是他也同时提到,可以仅仅使用一次逆向DFS做出来。我和同学研究了很久,我选择了正向DFS使用了两个优先队列共同维护路径上一个深度的颜色字典序排名,和这个深度的颜色的字典序。但是因为迷之原因,虽然能过样例,但是一直RE?后来选择了了大佬同学的逆向DFS法。(等下再讲)这里也贴出来,看看有没有大牛帮我看看那里有问题:

#include<algorithm>
#include<iostream>
#include<cstring>
#include<string>
#include<cstdio>
#include<vector>
#include<stack>
#include<cmath>
#include<queue>
#include<set>
#include<map>

using namespace std;
struct node{
	int point;
	int befo;
	int rank;
	int step;
	int last;
	int no;
	friend bool operator<(node a,node b)
	{
		if(a.step==b.step)
		{
			if(a.rank==b.rank)
			{
				return a.befo>b.befo;
			}
			return a.rank>b.rank;
		}
		return a.step>b.step;
	}
};
map<long long,int> flag;
priority_queue<node> q,p;
int tot;
const int size=1e5+5;
int roar[size];
node ex[10*size];
int n,m;
int fir[size],nxt[4*size],beg[4*size],en[4*size],col[4*size];
void edge_build(int b,int e,int c)
{
	beg[tot]=b;
	en[tot]=e;
	col[tot]=c;
	nxt[tot]=fir[b];
	fir[b]=tot++;
}
long long Hash(long long x,int y)
{
	return x*size+y;
}
int ccnt=0;
node bfs()
{
	while(!q.empty()) q.pop();
	node u;
	u.point=1;
	u.step=0;
	
	u.rank=1;
	u.no=0;
	ex[0]=u;
	q.push(u);
	
	while(!q.empty())
	{
		while(!q.empty())
		{
			node s=q.top();
			q.pop();
			if(s.point==n) return s;
			for(int i=fir[s.point];i!=-1;i=nxt[i])
			{
				if(!flag.count(Hash(beg[i],en[i])))
				{
					node t;
					t.point=en[i];
					t.step=s.step+1;
					t.no=ccnt;
					t.last=s.no;
					//cout<<en[i]<<' '<<beg[i]<<endl;
					t.befo=col[i];
					t.rank=s.rank;
					
					flag[Hash(beg[i],en[i])]=1;
					if(ccnt>=10*size) return t;
					ex[ccnt++]=t;
					p.push(t);
				}
			}
		}
			int cnt=0,estrank=0,estbefo=0;
			while(!p.empty())
			{
				node temp=p.top();
				p.pop();
				if(temp.rank>estrank)
				{
					cnt++;
					estrank=temp.rank;
					estbefo=0;
				}
				else if(estrank==temp.rank&&temp.befo>estbefo)
				{
					cnt++;
					estbefo=temp.befo;
				}
				temp.rank=cnt;
				q.push(temp);
			}
		
	}
}
int main()
{
	while(cin>>n>>m)
	{
		tot=0;
		flag.clear();
		int i;
		ccnt=1;
		memset(fir,-1,sizeof(fir));
		memset(nxt,0,sizeof(nxt));
		memset(roar,0,sizeof(roar));
		memset(ex,0,sizeof(ex));
		for(i=0;i<m;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			edge_build(a,b,c);
			edge_build(b,a,c);
		}
		node pp=bfs();
		int cnt=0;
		cout<<pp.step<<endl;
		//cout<<pp.last<<endl;
		for(i=pp.no;i!=0;i=ex[ex[i].last].no)
		{
			//cout<<i<<endl;
			roar[cnt++]=ex[i].befo;
		}
		//cout<<'x'<<endl;
		for(i=cnt-1;i>=0;i--)
		{
			printf("%d",roar[i]);
			if(i!=0) printf(" ");
			else puts("");
		}
	}
	return 0;
}

同学的想法是逆向DFS,如果在搜索的过程中遇到了已经搜索过的点,且其到目标节点的距离与其通过当前节点向目标节点的长度相等则不断向目标节点更新,找出字典序更小的那条路,这就是这个点到目标节点字典序最小且最短的路,如未搜索过这个点,则记录上这个点到目标节点的距离,最终达到根节点,详情见代码:

AC代码:

#include<iostream>
#include<queue>
#include<cstdio>
#include<set>
#include<cstring>
using namespace std;
struct node{
	int b,e,c;
};
typedef node edge;
const int size=1e5+5;
edge roar[4*size];
int first[size],nxt[size*4]; 
int tot=0;
int n,m;
set<int> flag;
int len[size];
int befo[size];
void edge_build(int b,int e,int c)
{
	roar[tot].b=b;
	roar[tot].e=e;
	roar[tot].c=c;
	nxt[tot]=first[b];
	first[b]=tot++;
}
queue<int> q;
void bfs()
{
	flag.clear();
	q.push(n);
	flag.insert(n);
	len[n]=0;
	while(!q.empty())
	{
		int s=q.front();
		q.pop();
		for(int i=first[s];i!=-1;i=nxt[i])
		{
			int b=roar[i].b;
			int e=roar[i].e;
			if(flag.count(e))
			{
				if(len[s]+1==len[e])
				{
					int j=befo[e];
					int k=i;
					while(j!=k&&roar[j].c==roar[k].c) j=befo[roar[j].b],k=befo[roar[k].b];
					if(roar[j].c>roar[k].c) befo[e]=i;
				}
			}
			else
			{
				flag.insert(e);
				befo[e]=i;
				len[e]=len[s]+1;
				q.push(e);
			}
		}
	}
}

int main()
{
	while(~scanf("%d%d",&n,&m))
	{
		memset(first,-1,sizeof(first));
		memset(nxt,0,sizeof(nxt));
		tot=0;
		for(int i=0;i<m;i++)
		{
			int a,b,c;
			scanf("%d%d%d",&a,&b,&c);
			edge_build(a,b,c);
			edge_build(b,a,c);
		}
		bfs();
		cout<<len[1]<<endl;
		for(int i=befo[1];len[1]--;i=befo[roar[i].b])
		{
			cout<<roar[i].c;
			if(len[1]) printf(" ");
			else printf("\n");
		}
	}
}

 

转载于:https://www.cnblogs.com/fly-white/p/10092769.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值