1+ import java .io .*;
2+ import java .util .*;
3+ public class dining {
4+ public static final int NO_PARENT = -1 ; // Constant for no parent
5+ public static void main (String [] args ) throws IOException {
6+ BufferedReader f = new BufferedReader (new FileReader ("dining.in" ));
7+ StringTokenizer st = new StringTokenizer (f .readLine ());
8+ int N ,M ,K ;
9+ N = Integer .parseInt (st .nextToken ());
10+ M = Integer .parseInt (st .nextToken ());
11+ K = Integer .parseInt (st .nextToken ());
12+ final int numOfCows = N - 1 ;
13+ List <List <Integer >> graph = new ArrayList <>(N );
14+ for (int i = 0 ; i < N ; i ++) {
15+ graph .add (new ArrayList <>());
16+ }
17+ Map <Pair ,Integer > cost = new HashMap <>();
18+ for (int i =0 ; i < M ; i ++) {
19+ st = new StringTokenizer (f .readLine ());
20+ int start = Integer .parseInt (st .nextToken ());
21+ int end = Integer .parseInt (st .nextToken ());
22+ int edgeCost = Integer .parseInt (st .nextToken ());
23+ graph .get (start -1 ).add (end -1 );
24+ graph .get (end -1 ).add (start -1 );
25+ cost .put (new Pair (start -1 ,end -1 ), edgeCost );
26+ }
27+ Map <Pair ,Integer > costWithHaybales = new HashMap <>(cost );
28+ System .out .println ("Cost without haybales: " +cost );
29+ System .out .println ("Graph as an edgelist: " +graph );
30+ //int[] taste = new int[N];
31+ Pair key ;
32+ //cost.get(new Pair(0,1));
33+ for (int i = 0 ; i < K ; i ++) {
34+ st = new StringTokenizer (f .readLine ());
35+ int index = Integer .parseInt (st .nextToken ()) - 1 ;
36+ int value = Integer .parseInt (st .nextToken ());
37+ //costWithHaybales.get(new Pair(0,1));
38+ for (int node : graph .get (index )) {
39+ key = new Pair (index ,node );//assert key.equals(new Pair(node,index));
40+ //System.out.println("Map: "+costWithHaybales);
41+ //System.out.println(key+ " "+costWithHaybales.get(key));
42+ costWithHaybales .put (key ,
43+ costWithHaybales .get (key ) - value );
44+ }
45+ //taste[index] = value;
46+ }
47+ System .out .println ("Cost with haybales: " +costWithHaybales );
48+ f .close ();
49+ int [] empty = new int [M ];
50+ Arrays .fill (empty , Integer .MAX_VALUE );
51+ int [] distTo = new int [M ];
52+ for (int i = 0 ; i < numOfCows ; i ++) {
53+ distTo = Arrays .copyOf (empty , M );
54+ }
55+ }
56+
57+ }
58+
59+ // Order does not matter pair
60+ class Pair {
61+ int x ,y ;
62+ public Pair (int x ,int y ) {
63+ this .x =x ;
64+ this .y =y ;
65+ }
66+ @ Override
67+ public String toString () {
68+ return "(" +this .x +"," +this .y +")" ;
69+ }
70+ @ Override
71+ public boolean equals (Object obj ) {
72+ if (obj instanceof Pair ) {
73+ Pair p = (Pair ) obj ;
74+ if ((p .x == this .x && p .y == this .y ) || (p .x == this .y && this .x == p .y )) {
75+ return true ;
76+ }
77+ }else {
78+ return false ;
79+ }
80+ return false ;
81+ }
82+ @ Override
83+ public int hashCode (){
84+ return (Integer .hashCode (this .x ) + 3 ) * (Integer .hashCode (this .y ) + 3 );
85+ }
86+ }
87+ class Edge {
88+ int x ;
89+ int y ;
90+ int weight ;
91+ public Edge (int x ,int y ,int weight ) {
92+ this .x = x ;
93+ this .y =y ;
94+ this .weight = weight ;
95+ }
96+ public String toString () {
97+ return "(" +this .x +"--" +this .y +" = " +this .weight +")" ;
98+ }
99+ @ Override
100+ public boolean equals (Object obj ) {
101+ if (obj instanceof Edge ) {
102+ Edge other = (Edge ) obj ;
103+ if ((this .x == other .x && this .y == other .y ) || (this .x == other .y && this .y == other .x )) {
104+ if (this .weight == other .weight ) {
105+ return true ;
106+ }else {
107+ return false ;
108+ }
109+ }
110+ return false ;
111+ }else {
112+ return false ;
113+ }
114+ }
115+ }
0 commit comments