清华OJ旅行商(TSP)

题目

旅行商(TSP)

Description
Shrek is a postman working in the mountain, whose routine work is sending mail to n villages. Unfortunately, road between villages is out of repair for long time, such that some road is one-way road. There are even some villages that can’t be reached from any other village. In such a case, we only hope as many villages can receive mails as possible.
Shrek hopes to choose a village A as starting point (He will be air-dropped to this location), then pass by as many villages as possible. Finally, Shrek will arrived at village B. In the travelling process, each villages is only passed by once. You should help Shrek to design the travel route.
Input
There are 2 integers, n and m, in first line. Stand for number of village and number of road respectively.
In the following m line, m road is given by identity of villages on two terminals. From v1 to v2. The identity of village is in range [1, n].
Output
Output maximum number of villages Shrek can pass by.
Example
Input
4 3
1 4
2 4
4 3
Output
3
Restrictions
1 <= n <= 1,000,000
0 <= m <= 1,000,000
These is no loop road in the input.
Time: 2 sec
Memory: 256 MB
Hints
Topological sorting
描述
Shrek是一个大山里的邮递员,每天负责给所在地区的n个村庄派发信件。但杯具的是,由于道路狭窄,年久失修,村庄间的道路都只能单向通过,甚至有些村庄无法从任意一个村庄到达。这样我们只能希望尽可能多的村庄可以收到投递的信件。
Shrek希望知道如何选定一个村庄A作为起点(我们将他空投到该村庄),依次经过尽可能多的村庄,路途中的每个村庄都经过仅一次,最终到达终点村庄B,完成整个送信过程。这个任务交给你来完成。
输入
第一行包括两个整数n,m,分别表示村庄的个数以及可以通行的道路的数目。
以下共m行,每行用两个整数v1和v2表示一条道路,两个整数分别为道路连接的村庄号,道路的方向为从v1至v2,n个村庄编号为[1, n]。
输出
输出一个数字,表示符合条件的最长道路经过的村庄数。
样例
见英文题面
限制
1 ≤ n ≤ 1,000,000
0 ≤ m ≤ 1,000,000
输入保证道路之间没有形成环
时间:2 sec
空间:256 MB
参考:参考

我的代码:

#include<cstdio>
#define MAXSIZE 1000000
#define GetMax(a,b) a>b?a:b
//注意全局变量数组的元素都会自动初始化为0

using namespace std;
struct ENode{
	int vsub;
	ENode *succ;
};

struct VNode{
	int in,len;//相对于邻接表结构,额外添加了属性len以计算到每个顶点的最大路径
	ENode *fstEdge;
};

VNode adjList[MAXSIZE];ENode *t;
int visited[MAXSIZE],stack[MAXSIZE],top=0,maxlen=0,n,e,i,j;
void TSort(){
	for(int k=0;k<n;k++)if(!adjList[k].in)stack[++top]=k;
	while(top){
		int v=stack[top--];
		for(ENode *p=adjList[v].fstEdge;p;p=p->succ){
//策略:动态规划-不断的更新到每个入度为0的顶点的邻居的最大路径长度
//相对于拓扑排序,额外添加了下述2句以更新到每个邻居的最大路径长度和记录图的最大路径长度
			adjList[p->vsub].len=GetMax(adjList[v].len+1,adjList[p->vsub].len);
			maxlen=GetMax(adjList[p->vsub].len,maxlen);
			if(!(--adjList[p->vsub].in))stack[++top]=p->vsub;
		}
	}
}

int main(){
	scanf("%d%d",&n,&e);
	for(int k=0;k<e;k++){
		scanf("%d%d",&i,&j);i--;j--;
		t=new ENode;t->vsub=j;adjList[j].in++;
		t->succ=adjList[i].fstEdge;adjList[i].fstEdge=t;
	}
	TSort();printf("%d\n",maxlen+1);//最后求的是最大路径包含的顶点数,故需要+1
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章