hdu 1625 Numbering Paths 最短路的变形,使用Floyd 外加判环

Numbering Paths

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 158    Accepted Submission(s): 47

Problem Description

Problems that process input and generate a simple ``yes‘‘ or ``no‘‘ answer are called decision problems. One class of decision problems, the NP-complete problems, are not amenable to general efficient solutions. Other problems may
be simple as decision problems, but enumerating all possible ``yes‘‘ answers may be very difficult (or at least time-consuming).

This problem involves determining the number of routes available to an emergency vehicle operating in a city of one-way streets.

Given the intersections connected by one-way streets in a city, you are to write a program that determines the number of different routes between each intersection. A route is a sequence of one-way streets connecting two intersections.

Intersections are identified by non-negative integers. A one-way street is specified by a pair of intersections. For example, j k indicates that there is a one-way street from intersection j to intersection k. Note that two-way streets can be modeled by specifying
two one-way streets: j k and k j .

Consider a city of four intersections connected by the following one-way streets:

0 1

0 2

1 2

2 3

There is one route from intersection 0 to 1, two routes from 0 to 2 (the routes are 0-1-2 and 0-2 ), two routes from 0 to 3, one route from 1 to 2, one route from 1 to 3, one route from 2 to 3, and no other routes.

It is possible for an infinite number of different routes to exist. For example if the intersections above are augmented by the street , there is still only one route from 0 to 1, but there are infinitely many different routes from 0 to 2. This is because the
street from 2 to 3 and back to 2 can be repeated yielding a different sequence of streets and hence a different route. Thus the route 0-2-3-2-3-2 is a different route than 0-2-3-2 .

Input

The input is a sequence of city specifications. Each specification begins with the number of one-way streets in the city followed by that many one-way streets given as pairs of intersections. Each pair j k represents a one-way street
from intersection j to intersection k. In all cities, intersections are numbered sequentially from 0 to the ``largest‘‘ intersection. All integers in the input are separated by whitespace. The input is terminated by end-of-file.

There will never be a one-way street from an intersection to itself. No city will have more than 30 intersections.

Output

For each city specification, a square matrix of the number of different routes from intersection j to intersection k is printed. If the matrix is denoted M, then M[j][k] is the number of different routes from intersection j to intersection
k. The matrix M should be printed in row-major order, one row per line. Each matrix should be preceded by the string ``matrix for city k‘‘ (with k appropriately instantiated, beginning with 0).

If there are an infinite number of different paths between two intersections a -1 should be printed. DO NOT worry about justifying and aligning the output of each matrix. All entries in a row should be separated by whitespace.

Sample Input

7 0 1 0 2 0 4 2 4 2 3 3 1 4 3
5
0 2
0 1 1 5 2 5 2 1
9
0 1 0 2 0 3
0 4 1 4 2 1
2 0
3 0
3 1

Sample Output

matrix for city 0
 0 4 1 3 2
 0 0 0 0 0
 0 2 0 2 1
 0 1 0 0 0
 0 1 0 1 0
matrix for city 1
 0 2 1 0 0 3
 0 0 0 0 0 1
 0 1 0 0 0 2
 0 0 0 0 0 0
 0 0 0 0 0 0
 0 0 0 0 0 0
matrix for city 2
 -1 -1 -1 -1 -1
 0 0 0 0 1
 -1 -1 -1 -1 -1
 -1 -1 -1 -1 -1
 0 0 0 0 0

不得不说,这题的输出格式就尼玛傻逼。PE了4,5次,每一行的第一个数就要空格。艹,最讨厌这样的题了,怪不得做的人少!

这题主要就是判环,如果mat[k][k]!=0,则K这点存在一个环,根据floyd的动态规划的含义,,mat[i][j]如果经过一个这样的k点的话,那mat[i][j]也是一个环,

直接代码:

#include <stdio.h>
#include <string.h>
#define MAX 1010
#define INF 500000000
int mat[MAX][MAX] ;
void floyd(int n)
{
	for(int k = 0 ; k <= n ; ++k)
	{
		for(int i = 0 ; i <= n ; ++i)
		{
			if(mat[i][k] == 0)
				continue ;
			for(int j = 0 ; j <= n ; ++j)
			{
				if(mat[k][j] != 0)	//判环。
				{
					if(mat[k][k] != 0 || mat[k][j] == -1 || mat[i][k] == -1)
					{
						mat[i][j] = -1 ;
					}
					else
					{
						mat[i][j] += mat[i][k]*mat[k][j] ;
					}
				}

			}
		}
	}
	for(int i = 0 ; i <= n ; ++i)
	{
		if(mat[i][i] > 0)
		{
			mat[i][i] = -1;
		}
	}
}

int main()
{
	int n ,t=0;
	while(scanf("%d",&n) != EOF )
	{
		int max = -INF ;
		memset(mat,0,sizeof(mat)) ;
		for(int i = 0 ; i < n ; ++i)
		{
			int x , y ;
			scanf("%d%d",&x,&y) ;
			mat[x][y] = 1 ;
			if(x>max||y>max)
			{
				max = x>y?x:y ;
			}
		}
		floyd(max) ;
		printf("matrix for city %d\n",t++);
		for(int i = 0; i <= max ; ++i)
		{
			for(int j = 0 ; j <= max ; ++j)
			{
				printf(" %d",mat[i][j]) ;
			}
			printf("\n") ;
		}
	}
	return 0 ;
}
时间: 2024-10-21 07:32:03

hdu 1625 Numbering Paths 最短路的变形,使用Floyd 外加判环的相关文章

2017多校第10场 HDU 6181 Two Paths 次短路

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=6181 题意:给一个图,求出次短路. 解法:我之前的模板不能解决这种图,就是最短路和次短路相等的情况,证明这题数据还是水了.下来我修改了一下次短路的,就可以避免这种情况了.提供一个sample 4 4 (1,2,1)( 1, 3,1) (2 4,1) (3 ,4,1).这组的正确答案是2,算法就来看代码吧. #include <bits/stdc++.h> using namespace std;

HDU 1317 XYZZY(floyd+bellman_ford判环)

http://acm.hdu.edu.cn/showproblem.php?pid=1317 题意: 给出一个有向图,每到达一个点,都会加上或减去一些能量,我们要做的就是判断从1出发是否能到达n.初始能量有100,行走的途中能量不能小于等于0. 思路: 首先我们用floyd来判断一下1和n之间是否有通路. 其次就是bellman_ford算法来判正环了. 1 #include <iostream> 2 #include <cstring> 3 #include <algori

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

HDU 3832 Earth Hour (最短路)

Earth Hour Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 125536/65536 K (Java/Others)Total Submission(s): 1518    Accepted Submission(s): 607 Problem Description Earth Hour is an annual international event created by the WWF (World Wide Fun

HDU 1491 社交网络(最短路计数)

题目链接:http://61.187.179.132/JudgeOnline/problem.php?id=1491 题意:给出一个联通的无向图,边有权值.定义I(v)如下,计算每个点的I值. 思路:(1)最简单的就是那个floyd了...g[i][j]记录最短路长度,f[i][j]记录个数,不多说了: (2)下面说说我自己想出来的算法..枚举s和t,计算每个点到s和t的最短路..然后建立最短路树..之后求s到t的最短路个数..然后枚举删掉某个点再求最短路个数,则差值即为通过删掉点的最短路数目.

HDU 4856 Tunnels (最短路+状压DP)

题意:给你N*N的网格,'.'表示可以走,'#'表示不能走,m条管道,每条管道有起点和终点坐标, Bob每次可以走到相邻的网格花费1s,问Bob走完m条管道要花多少时间:Bob在管道内不计算时间 即计算Bob从管道 i 的出口走到管道 j 的入口的时间Dis(e[i],s[j])的最小和,起点可以任意: 思路:看了题解说是状态压缩DP然后深入理解了下. 首先算出d[e[i]][s[j]]的最短距离,不能到达为-1: dp[i][j] : 表示以 j 为起点状态为 i 的最小值.其中 i 是用十进

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

hdu 2962 Trucking (二分+最短路Spfa)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2962 Trucking Time Limit: 20000/10000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1763    Accepted Submission(s): 618 Problem Description A certain local trucking co

hdu 1690 Bus System(最短路)

问题: 链接:点击打开链接 题意: 思路: 代码: #include <iostream> #include <cstdio> #include <cstring> using namespace std; #define INF 1000000000000 typedef __int64 LL; const int N = 110; __int64 dis[N][N],place[N]; __int64 L1,L2,L3,L4,C1,C2,C3,C4; int n,m