关于一个图算法的应用

/**
 * 
 * @author SHQ
 *编程题 | 30分 1/2
 *题目描述:
	小A是一个城市中救援机构的负责人,他手头上有一张特殊的地图,记载了本省各个城市中救援队伍的数量和城市之间的道路连接情况。
	他的任务相当重要,除负责本市紧急情况救援外,他还需要在某个其他城市发生紧急情况时,第一时间带领他的队伍参与救援,
	同时调动路过城市的救援机构参与救援。
输入
	多组输入数据,第一行是一个正整数T(T<=10),表示测试数据的组数,接下来包含T组测试数据。
	每组测试数据的第一行包含四个整数n(1<n<=1000),m(0<=m<=n*(n-1)/2),s 和 t(1<=s<=n, 1<=t<=n),分别表示城市的数量、道路的数量、
	小A所在的城市和目标城市。
	第二行包含n个整数,表示每个城市中救援机构的数量。
	接下来的M行中,每行包含三个整数u, v, w, (0 <= u <= n-1, 0 <= v <= n-1, u不等于v, 0 <= w <= 1)
	表示结点u和结点v之间存在一条长度为w(1<=w<=10000的道路(保证没有重复)。
	不保证城市之间有道路可以通行。
输出
	对于每一组测试数据,输出一行"Case t: x y",其中 t表示第t组数据,t从1开始递增。x从小A所在城市到救援城市之间可以通行的路径数
	,y为所能召集的最大救援队伍数。若无法达到救援目标城市,则输出"No answer",详见给定样例。
	
样例输入
	2
	4 5 0 3
	1 1 1 1
	0 1 1
	0 2 3
	1 2 2
	1 3 3
	2 3 1
	5 4 0 4
	1 1 1 1 1
	0 1 1
	1 2 3
	0 2 2
	3 4 3
样例输出
	Case 1: 3 4
	Case 2: No answer
 *
 */


import java.util.*;

class Main{
    public static class Node{
        public int val;
        public int in;//入度
        public int out;//出度
        public ArrayList<Node> nexts;
        public ArrayList<Edge> edges;
        public Node(int val){
            this.val=val;
            in=0;
            out=0;
            nexts=new ArrayList<Node>();
            edges=new ArrayList<Edge>();
        }
    }
    public static class Edge{
        public int weight;
        public Node from;//入度
        public Node to;//出度
        public Edge(Node from,Node to,int weight){
            this.weight=weight;
            this.from=from;
            this.to=to;
        }
    }
    public static class Graph{
        public HashMap<Integer,Node> nodes;
        public HashSet<Edge> edges;
        public Graph(){
            nodes=new HashMap<Integer,Node>();
            edges=new HashSet<Edge>();
        }
    }
    //用一个二维数组来表示图,格式为: 权值 = matrix[0][0]  from点 = matrix[0][1]  to点 = matrix[0][2]
    public static void initGraph(int[][] matrix,Graph graph){
        for (int i=0;i<matrix.length;i++){
            int from=matrix[i][0];
            int to = matrix[i][1];
            int weight = matrix[i][2];

            if (!graph.nodes.containsKey(from)){
                graph.nodes.put(from,new Node(from));
            }
            if (!graph.nodes.containsKey(to)){
                graph.nodes.put(to,new Node(to));
            }
            Node fromNode = graph.nodes.get(from);
            Node toNode = graph.nodes.get(to);
            Edge edge = new Edge(fromNode,toNode,weight);
            fromNode.nexts.add(toNode);
            fromNode.out=fromNode.out+1;
            toNode.in=toNode.in+1;
            fromNode.edges.add(edge);
            graph.edges.add(edge);
        }
    }


    static int res=0;
    static int max=0;
    public static void main(String[] args){
        Scanner in = new Scanner(System.in);
        int n = in.nextInt();
        int m= in.nextInt();
        int s = in.nextInt();
        int t = in.nextInt();
        int[] savenums = new int[n];
        int[][] maxtire = new int[m][3];
        for (int i=0;i<n;i++){
            savenums[i] = in.nextInt();
        }
        for (int i=0;i<m;i++){
            maxtire[i][0]=in.nextInt();
            maxtire[i][1]=in.nextInt();
            maxtire[i][2]=in.nextInt();
        }
        Graph graph = new Graph();
        initGraph(maxtire,graph);
        Node start = graph.nodes.get(s);
        Node end = graph.nodes.get(t);
        dfs(start,end,0,savenums);
        System.out.println(res);
        System.out.println(max);

    }

    public static void  dfs(Node now,Node end,int sum,int[] savenums){
        if (now.val==end.val){
            max=Math.max(max,sum+savenums[now.val]);
            res=res+1;
            return ;
        }
        if (now.nexts==null){
            return;
        }
        for (Node next:now.nexts){
            dfs(next,end,sum+savenums[next.val],savenums);
        }

    }


}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值