SW练习_pro20200109_小石头游戏_拓扑排序

package com.company.real;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.InputStreamReader;
import java.util.*;
/*
2
4 3 2
1 2 2 1
2 3 3 4
3 4 5 4
2 1 2
1 2 5 7
*/
//SW练习_pro20200109_小石头游戏_拓扑排序
public class P20200109 {
    static int[] in;
    //static int[] out;
    static List<Integer>[] f;//list[i].get(j) 保存的是第i个节点链接的第j个点的编号
    public static void main(String[] args) throws Exception{
        //System.setIn(new FileInputStream("C:\\Users\\XAGDC\\Desktop\\sw\\PRO\\47 Pro 小石头游戏 The Pebble Game\\sample_input.txt"));
        System.setIn(new FileInputStream("C:\\Users\\XAGDC\\Desktop\\sw\\PRO\\47 Pro 小石头游戏 The Pebble Game\\eval_input.txt"));
        BufferedReader reader=new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer st=new StringTokenizer(reader.readLine());
        int T=Integer.parseInt(st.nextToken());
        for (int zz = 0; zz <T ; zz++) {
            List<Integer> res=new ArrayList<>();
            st=new StringTokenizer(reader.readLine());
            int N=Integer.parseInt(st.nextToken());//石子的个数
            int M=Integer.parseInt(st.nextToken());//线的个数
            int K=Integer.parseInt(st.nextToken());//第几个石子
            f=new List[N+1];
            in=new int[N+1];
            //out=new int[N+1];
            PriorityQueue<Integer> pq=new PriorityQueue();//用来存没有入度的点
            for (int i = 1; i <=N ; i++) {
                f[i]=new ArrayList();
                f[i].add(0);
            }
            long sum=0;
            for (int i = 0; i <M ; i++) {
                st=new StringTokenizer(reader.readLine());
                int a=Integer.parseInt(st.nextToken());//点1的编号
                int b=Integer.parseInt(st.nextToken());//点2的编号
                int p=Integer.parseInt(st.nextToken());//点1的分数
                int q=Integer.parseInt(st.nextToken());//点2的分数
                if(p>q){//p->q  a->b
                    //out[a]++;
                    in[b]++;
                    f[a].add(b);
                    sum=sum+p;
                }else if(p<q){//q->p  b->a
                    //out[b]++;
                    in[a]++;
                    f[b].add(a);
                    sum=sum+q;
                }else{
                    sum=sum+q;
                }
            }
            for (int i = 1; i <=N ; i++) {
                if(in[i]==0){//找到入度为0的节点
                    pq.add(i);
                }
            }
            while(!pq.isEmpty()){
                int c=pq.poll();//取出当前节点
                res.add(c);
                //for (int i = 1; i <=out[c] ; i++) {//这个点的连接节点入度-1
                for (int i = 1; i <f[c].size() ; i++) {//这个点的连接节点入度-1
                    in[f[c].get(i)]--;
                    if(in[f[c].get(i)]==0){//如果减完后,这个点入度为0了
                        pq.add(f[c].get(i));
                    }
                }
            }
            System.out.printf("#%d %d %d\n",(zz+1),sum,res.get(K-1));
        }
        reader.close();
    }
}

 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值