#! /usr/bin/python

# Takes an instance in .ins format with uniform weights
# and adds random weights from [1,10000]  

from random import *

def trans(infile):
	edges=[]    
	tree=[]    
	n=0
	fd = open(infile, "r")
	#fil= fd.readlines()
	for line in fd:
	    if line[0]=="p":
		   h=line[2:].strip().split()
		   n=int(h[1])
            elif line[0]=="t":
		   h=line[2:].strip().split()
		   ex=[int(h[0]),int(h[1]),0]
		   tree.append(ex)
	    elif line[0]=="e":
		   h=line[2:].strip().split()
		   ex=[int(h[0]),int(h[1]),int(h[2])]
		   edges.append(ex)
	#n = eval(fil[0].strip())
	#edges= eval(fil[2].strip())
	fd.close

	ran=Random()
	ran.seed(str(sys.argv[2]))
	fd = open(infile[:-4]+".ins","w")
	fd.write("p "+infile+" "+str(n)+" "+str(len(edges)+len(tree))+'\n')
	#fd.write("dummy line"+'\n')
	for e in edges:
		cost=int(ran.random()*10000)+1
		fd.write("e "+str(e[0])+" "+str(e[1])+" "+str(cost)+'\n')
	for e in tree:
		fd.write("t "+str(e[0])+" "+str(e[1])+'\n')
	fd.close()
	print infile,'done'
	return



import sys
trans(sys.argv[1])


#import glob
#b=glob.glob("*.ins")
#import dircache
#k=dircache.listdir(".")
#kn=k[:]
#kn.remove('add1.py')
#for a in b: trans(a)


#if __name__ == "__main__":

	
