POJ3255 Roadblocks 次短路Dijkstra做法

有段时间没做题了,这几天一直在寻找感觉,尽量多看书,这题目就是n个地方,编号从1到n,然后有r条路,问你从1号到达n号地方的次短路长度为多少,同一条边可以重复走,直接在dijkstra算法里同时记录一个次短路就可以了,但是一直WA,后来去看了讨论面板,那里有人给了测试数据,我不知道那数据的对错,但是干扰了我很久,也许那些数据实在题目案例之外的吧,对我的程序没有任何影响,我只是太久没错 一时 忘了双向边了,一开始只建立了单向边~代码写的也比较冗长~

int n,r;

typedef struct Node {
	int to;
	int val;
	Node(int to,int val):to(to),val(val){}
};

vector<Node> G[10000 + 55];

int dis1[10000 + 55];

int dis2[10000 + 55];

void init() {
	for(int i=0;i<10000 + 55;i++) {
		dis1[i] = inf;
		dis2[i] = inf;
		G[i].clear();
	}
}

bool input() {
	while(cin>>n>>r) {
		for(int i=0;i<r;i++) {
			int u,v,w;
			cin>>u>>v>>w;
			u--,v--;
			G[u].push_back(Node(v,w));
			G[v].push_back(Node(u,w));
		}
		return false;
	}
	return true;
}

void dijkstra() {
	priority_queue<pair<int ,int >,vector<pair<int ,int >>,greater<pair<int ,int >> > q;
	dis1[0] = 0;
	q.push(make_pair(0,0));
	while(!q.empty()) {
		pair<int ,int > p = q.top();
		q.pop();
		int v = p.second;
		int d = p.first;
		if(dis2[v] < d)continue;
		for(int i=0;i<G[v].size();i++) {
			Node e = G[v][i];
			int dis = d + e.val;
			if(dis1[e.to] > dis) {
				swap(dis1[e.to],dis);
				q.push(make_pair(dis1[e.to],e.to));
			}
			if(dis2[e.to] > dis && dis1[e.to] < dis) {
				dis2[e.to] = dis;
				q.push(make_pair(dis2[e.to],e.to));
			}
		}
	}
}

void cal() {
	dijkstra();
	cout<<dis2[n - 1]<<endl;
}

void output() {

}

int main() {
	while(true) {
		init();
		if(input())return 0;
		cal();
		output();
	}
	return 0;
}
时间: 2024-10-08 03:15:38

POJ3255 Roadblocks 次短路Dijkstra做法的相关文章

POJ3255:Roadblocks(次短路 SPFA+A星)

给出1-N 个点 的距离, 求从1号到N号的次短路, 直接用k短路来做了,,dj会TLE, 用spfa就过了 题目: I - RoadblocksTime Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Description Bessie has moved to a small farm and sometimes enjoys returning to visit o

poj3255 Roadblocks 次短路

Roadblocks Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10098   Accepted: 3620 Description Bessie has moved to a small farm and sometimes enjoys returning to visit one of her best friends. She does not want to get to her old home too

bzoj1726 [Usaco2006 Nov]Roadblocks第二短路

1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 826  Solved: 394[Submit][Status] Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路. 贝茜所在的乡村有R(1<=R<=100,000)条双向

1726: [Usaco2006 Nov]Roadblocks第二短路

1726: [Usaco2006 Nov]Roadblocks第二短路 Time Limit: 5 Sec  Memory Limit: 64 MBSubmit: 835  Solved: 398[Submit][Status] Description 贝茜把家搬到了一个小农场,但她常常回到FJ的农场去拜访她的朋友.贝茜很喜欢路边的风景,不想那么快地结束她的旅途,于是她每次回农场,都会选择第二短的路径,而不象我们所习惯的那样,选择最短路. 贝茜所在的乡村有R(1<=R<=100,000)条双向

BZOJ 1726: [Usaco2006 Nov]Roadblocks第二短路( 最短路 )

从起点和终点各跑一次最短路 , 然后枚举每一条边 , 更新answer ----------------------------------------------------------------------------------------------------- #include<cstdio> #include<cstring> #include<algorithm> #include<iostream> #include<queue&g

POJ1062 Expensive dowry 【最短路dijkstra】

详细看:http://blog.csdn.net/lyy289065406/article/details/6645852 简单说一下:每个物品是一个结点,边的权值是,edge[u][v]的值表示用物品u换物品v的价格 一开始所有物品都置为原价,即所有dist[i]为原价,用dijkstra算法,算出0点(啥物品都没有)到各点的最短距离,求出dist[1]即为花费 枚举每个物品的等级为这条交易链的最高等级,把所有等级高于它的或者比它小超过等级限制的点都剔除,可以用bool数组剔除,然后用上述的d

POJ 2387 Til the Cows Come Home (最短路+Dijkstra)

Til the Cows Come Home Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 29550   Accepted: 9935 Description Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the

POJ-3268-最短路(dijkstra算法)

Silver Cow Party Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 12494   Accepted: 5568 Description One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X

hdu 2544 最短路 Dijkstra

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2544 题目分析:比较简单的最短路算法应用.题目告知起点与终点的位置,以及各路口之间路径到达所需的时间,要求输出起点到终点的最短时间. /* 最短路 Time Limit: 5000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submission(s): 35043 Accepted Submission