06-图8. 关键活动(30)

题目来源:

http://www.patest.cn/contests/mooc-ds/06-%E5%9B%BE8

解:

import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;


public class Main {

	public static void main(String[] args) {

		Scanner scanner = new Scanner(System.in);
		int N = scanner.nextInt();//点数
		int M = scanner.nextInt();//边数
		int[][] adjacency =new int[N+1][N+1]; //邻接表
		int[] indegree= new int[N+1];//节点的入度
		int[] outdegree = new int[N+1]; //节点的出度
		int[] visit= new int[N+1];//访问标志
		int[] lcost =new int[N+1];//最迟开始实践
		int[] ecost = new int[N+1];//最早开始时间
		int max =0;//最大值的下标
		ArrayList<Integer> list = new ArrayList<Integer>();//存储关键路径
		Arrays.fill(indegree, 0);
		Arrays.fill(outdegree, 0);
		Arrays.fill(visit, 0);
		Arrays.fill(lcost, 0);
		Arrays.fill(ecost, Integer.MAX_VALUE);
		for(int i=1;i<=N;++i)
			Arrays.fill(adjacency[i],Integer.MAX_VALUE); 
		//M次循环输入
		for(int i=1;i<=M;i++)
		{
			int start = scanner.nextInt();
			int end = scanner.nextInt();
			int cost = scanner.nextInt();
			adjacency[start][end]=cost;
			indegree[end]++; 
			outdegree[start]++;
		}
		//计算关键路径长度	
		//计算最晚开始时间
		for(int j=1;j<=N;++j)
		{
			int index =getZeroDegreeIndex(indegree,visit);
			if(index==-1)//没有度为0的点
			{
				System.out.println(0);
				return ;
			}
			else
			{
				int[] adj = adjacency[index]; //遍历节点index连接的所有节点
				for(int i=1;i<=N;++i)
				{
					if(adj[i]!=Integer.MAX_VALUE ) //如果可达
					{
						indegree[i]--;
						if(lcost[i] <lcost[index]+adj[i])
						{
							lcost[i] = lcost[index]+adj[i];
							if(lcost[max]<lcost[i])
								max=i;
						}
					}
				}
			}
			
		}
		//计算最早开始时间
		Arrays.fill(visit, 0);
		ecost[max]=lcost[max];
		for(int j=1;j<=N;++j)
		{
			int index =getZeroDegreeIndex(outdegree,visit);
			if(index==-1)//没有度为0的点
			{
				System.out.println(0);
				return ;
			}
			else
			{
				//遍历第index列
				for(int i=1;i<=N;++i)
				{
					int temp = adjacency[i][index];
					if(temp!=Integer.MAX_VALUE ) //如果可达
					{
						outdegree[i]--;
						if(ecost[i] > ecost[index]-temp)
						{
							ecost[i] = ecost[index]-temp;
							
						}
					}
				}
			}
			
		}
		System.out.println(lcost[max]);
		// 最早开始时间=最晚开始时间的为关键路径节点
		for(int i=1 ;i<=N;i++)
		{
			if(lcost[i]==ecost[i])
				list.add(i);
		}
		for(int i=1;i<=N;i++)
		{
			if(list.contains(i))
			{
				for(int j=N;j>=1;--j)
				{
					if(list.contains(j) && ecost[j]==ecost[i]+adjacency[i][j]) //如果可达
					{
						System.out.println(i+"->"+j);
					}
				}
			}
		}
		
		
		
	
		
		

		
	}
	/*寻找入度为0的点的下标
	 * */
	public static int getZeroDegreeIndex(int[] degree,int[] visit)
	{
		
		for(int i=1;i<degree.length;++i)
		{
			if(degree[i]==0 && visit[i]==0)
			{
				visit[i]=1;
				return i;
			}
				
		}
		return -1;
	}

}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值