SGU 145.Strange People

时间:0.25s空间:4m

题意:

     其实就是求无环第K短路。

输入:

     给出n,m,k,分别代表,n个点,m条边,第k长路。
     接下来m行,三个整数x,y,z,分别代表x,y之间有条费用为x的双向路。保证没有重边。

输出:

    第一行两个数a,b,第k小费用a,和经过的点的个数b。
    接下来b个数,代表第k短的路径。

Sample Input

 5 10 3
 1 2 6
 1 3 13
 1 4 18
 1 5 35
 2 3 14
 2 4 34
 2 5 17
 3 4 22
 3 5 15
 4 5 34
 1 5

Sample Output

 35 2
 1 5

Solution:         求k短路的算法,基本都是A*,这里的数据量比较小,可以采用二分答案。         二分路径的长度,DFS求出有多少条路径的长度小于它,如果是(k-1)的话直接输出。

 PS:sgu在这一题上数据似乎出了问题,很多人都PE没法AC,我试着提交别人AC过的代码还是pe。    因此代码没有AC,但对程序的正确性有把握。

参考代码:
#include <cstdio>
const int INF = 111;
int g[INF][INF], vis[INF], path[INF];
int n, m, k, x, y, z, l, r, mid, leSum;
int S, T, pd, len, tol;
void dfs (int x, int dis) {
	vis[x] = 1;
	if (x == T) {
		if (dis < mid)  leSum++;
		if (!pd && dis == mid) leSum++, pd = 1;
	}
	else
		for (int i = 1; i <= n; i++)
			if (!vis[i] && g[x][i] && dis + g[x][i] <= mid)
				dfs (i, dis + g[x][i]);
	vis[x] = 0;
}
int check (int x) {
	pd = leSum = 0;
	dfs (S, 0);
	return leSum;
}
int getPath (int x, int dis) {
	vis[x] = 1;
	if (x == T && dis == len) {
		path[++tol] = x;
		return pd = 1;
	}
	else
		for (int i = 1; i <= n; i++) {
			if (!vis[i] && g[x][i] && g[x][i] + dis <= len) {
				if (getPath (i, dis + g[x][i]) ) path[++tol] = x;
				if (pd) return 1;
			}
		}
	vis[x] = 0;
}
int Search () {
	while (l <= r) {
		mid = l + (r - l >> 1);
		int tem = check (mid);
		if (tem == k)
			return mid;
		else if (tem > k)
			r = mid - 1;
		else l = mid + 1;
	}
	return -1;
}
int main() {
	scanf ("%d %d %d", &n, &m, &k);
	for (int i = 1; i <= m; i++) {
		scanf ("%d %d %d", &x, &y, &z);
		g[x][y] = g[y][x] = z, r += z;
	}
	scanf ("%d %d", &S, &T);
	len = Search();
	pd = tol = 0;
	getPath (S, 0);
	printf ("%d %d\n", len, tol);
	for (int i = tol; i > 1; i--)
		printf ("%d ", path[i]);
		printf("%d\n",path[1]);
	return 0;
}

  


  


SGU 145.Strange People

时间: 2024-10-11 14:31:33

SGU 145.Strange People的相关文章

SGU 145

节点不可重复经过的K短路问题. 思路:二分路径长度,深搜小于等于路径长度的路径数.可以利用可重复点K短路问题中的A*函数进行剪枝. 尝试另一种解法:把可重复点K短路A*直接搬过来,堆中的每个元素额外记录之前走过的所有点.这样就可以据此防止走重复的点.最大100个点,可用两个长整形状态压缩. 一直PE,无法验证效率. 1 #include <map> 2 #include <set> 3 #include <list> 4 #include <queue> 5

SGU 438 The Glorious Karlutka River =) 拆点+动态流+最大流

The Glorious Karlutka River =) Time Limit:500MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 438 Appoint description: Description A group of Mtourists are walking along the Karlutka river. They want to cross

sgu Kalevich Strikes Back

这道题就是求一个大矩形被n个矩形划分成n+1个部分的面积,这些矩形之间不会相交,可能包含.. 1 #include <cstdio> 2 #include <cstring> 3 #include <vector> 4 #include <algorithm> 5 #define maxn 120100 6 using namespace std; 7 8 long long s[maxn]; 9 vector<int>g[maxn]; 10 i

Codeforces Round #425 (Div. 2) Problem C (Codeforces 832C) Strange Radiation - 二分答案 - 数论

n people are standing on a coordinate axis in points with positive integer coordinates strictly less than 106. For each person we know in which direction (left or right) he is facing, and his maximum speed. You can put a bomb in some point with non-n

hdu 5152 A Strange Problem线段树+欧拉函数

*****************************************BC题解**********************************************************************1003 A Strange Problem 对于操作二,利用公式 当x >= Phi(C), A^x = A ^ (x%Phi(C) + Phi(C)) (mod C) 对于2333333这个模数来说,求18次欧拉函数后就变成了1,所以只需要保存19层第三次操作的加数

sgu 463 - Walking around Berhattan

K - Walking around Berhattan Time Limit:250MS     Memory Limit:65536KB     64bit IO Format:%I64d & %I64u Submit Status Practice SGU 463 Description As you probably know, Berhattan is a district of Berland's largest city and it consists of equal squar

Cacti Error:&#39;145“解决

早上正常查看监控之一,cacti不出图了.昨天断电了一次.排查: 1.检查服务正常 2.检查服务的连接,正常 3.查看日志,报错145. 日志报错如下: AM - CMDPHP: Poller[0] ERROR: SQL Assoc Failed!, Error:'145', SQL:"select  poller_output.output,  poller_output.time,  poller_output.local_data_id,  poller_item.rrd_path,  p

【SGU 390】Tickets (数位DP)

Tickets Description Conductor is quite a boring profession, as all you have to do is just to sell tickets to the passengers. So no wonder that once upon a time in a faraway galaxy one conductor decided to diversify this occupation. Now this conductor

ACM: SGU 101 Domino- 欧拉回路-并查集

sgu 101 - Domino Time Limit:250MS     Memory Limit:4096KB     64bit IO Format:%I64d & %I64u Description Dominoes – game played with small, rectangular blocks of wood or other material, each identified by a number of dots, or pips, on its face. The bl