UVa 988 - Many Paths, One Destination

题目:人生有很多选择,现在给你一些选择(0~n-1),和每个选择分支后面的其他选择序号,求选择总数。

分析:dp,图论。如果某状态的后续选择个数是0个则,代表死亡,统计所有到达死亡的路径条数即可。

用一个状态数组记录到达每个选择的路径数,它等于能到达它的前驱节点的路径加和。

稀疏图,使用邻接表储存。初始是节点0的路径条数为1,代表出生。

说明:没有给数据范围略坑啊,RE一次,WA一次,╮(╯▽╰)╭。

#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cstdio>

using namespace std;

int sums[15000];
int deat[15000];

typedef struct tnode
{
	int    point;
	tnode *next;
}node;
node* H[15000];
node  E[150000];
int   e_size = 0;

void initial()
{
	memset( H, 0, sizeof(H) );
	memset( E, 0, sizeof(E) );
	e_size = 0;
}

void addedge( int a, int b )
{
	E[e_size].point = b;
	E[e_size].next = H[a];
	H[a] = &E[e_size ++];
}

int main()
{
	int n,m,c,t = 1;
	while ( ~scanf("%d",&n) ) {
		if ( t ++ > 1 ) printf("\n");
		initial();
		memset( sums, 0, sizeof(sums) );
		for ( int i = 0 ; i < n ; ++ i ) {
			scanf("%d",&m);
			for ( int j = 0 ; j < m ; ++ j ) {
				scanf("%d",&c);
				addedge( i, c );
			}
			if ( !m ) deat[i] = 1;
			else deat[i] = 0;
		}

		sums[0] = 1;
		for ( int i = 0 ; i < n ; ++ i )
		for ( node* p = H[i] ; p ; p = p->next )
			sums[p->point] += sums[i];
		int sum = 0;
		for ( int i = 1 ; i < n ; ++ i )
			if ( deat[i] )
				sum += sums[i];

		printf("%d\n",sum);
	}
	return 0;
}

UVa 988 - Many Paths, One Destination

时间: 2024-10-17 19:51:15

UVa 988 - Many Paths, One Destination的相关文章

uva 125 Numbering Paths(warshall算法)

uva 125 Numbering Paths Description Download as PDF Background Problems that process input and generate a simple yes'' orno" answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general ef

UVA 10564 十 Paths through the Hourglass

Paths through the Hourglass Time Limit:3000MS     Memory Limit:0KB     64bit IO Format:%lld & %llu Submit Status Practice UVA 10564 1 #include <stdio.h> 2 #include <string.h> 3 4 int n,s; 5 long long dp[55][56][550]; 6 int a[56][55]; 7 8 i

UVa 10564 DP Paths through the Hourglass

从下往上DP,d(i, j, k)表示第(i, j)个格子走到底和为k的路径条数. 至于字典序最小,DP的时候记录一下路径就好. 1 #include <cstdio> 2 #include <cstring> 3 #include <algorithm> 4 using namespace std; 5 6 int n, sum; 7 int a[50][25]; 8 long long d[50][50][550]; 9 int p[50][50][550][2];

On-demand diverse path computation for limited visibility computer networks

In one embodiment, a source device detects a packet flow that meets criteria for multi-path forwarding, and forwards a probe packet on a primary path from the source device to a destination device, the probe packet carrying an indication to cause a p

UVA 10564 Paths through the Hourglass[DP 打印]

UVA - 10564 Paths through the Hourglass 题意: 要求从第一层走到最下面一层,只能往左下或右下走 问有多少条路径之和刚好等于S? 如果有的话,输出字典序最小的路径. f[i][j][k]从下往上到第i层第j个和为k的方案数 上下转移不一样,分开处理 没必要判断走出沙漏 打印方案倒着找下去行了,尽量往左走 沙茶的忘注释掉文件WA好多次 #include <iostream> #include <cstdio> #include <algor

uva 10564 Paths through the Hourglass (DP)

uva 10564 Paths through the Hourglass Paths through the Hourglass Input: Standard Input Output: Standard Output Time Limit: 2 Seconds In the hourglass to the right a path is marked. A path always starts at the first row and ends at the last row. Each

UVA - 12295 Optimal Symmetric Paths (递推)

Description  Optimal Symmetric Paths  You have a grid of n rows and n columns. Each of the unit squares contains a non-zero digit. You walk from the top-left square to the bottom-right square. Each step, you can move left, right, up or down to the ad

01背包(类) UVA 10564 Paths through the Hourglass

题目传送门 1 /* 2 01背包(类):dp[i][j][k] 表示从(i, j)出发的和为k的方案数,那么cnt = sum (dp[1][i][s]) 3 状态转移方程:dp[i][j][k] = dp[i+1][j][k-c] + dp[i+1][j+1][k-c];(下半部分) 上半部分类似 4 因为要输出字典序最小的,打印路径时先考虑L 5 */ 6 /************************************************ 7 * Author :Runni

Numbering Paths (Uva 125 floyd+dp思想)

Numbering Paths Time Limit: 3000MS   Memory Limit: Unknown   64bit IO Format: %lld & %llu Submit Status Description Background Problems that process input and generate a simple ``yes'' or ``no'' answer are called decision problems. One class of decis