A file reader: read the formated input data provided by Q2914: Minimum Cut
| 151 | |
| 152 | |
| 153 | class Reader(object): |
| 154 | ''' A file reader: read the formated input data provided by Q2914: Minimum Cut |
| 155 | ''' |
| 156 | def __init__(self, fd): |
| 157 | ''' @param fd: an opened file-like object, and its contents are the input data. |
| 158 | ''' |
| 159 | self.fd = fd |
| 160 | |
| 161 | def gen_graph(self): |
| 162 | ''' a generator that yield graphs in order |
| 163 | ''' |
| 164 | while True: |
| 165 | try: |
| 166 | line = self.fd.next() |
| 167 | nVertices, nEdges = map(int, line.split()) |
| 168 | if g_TryFlowNetwork: |
| 169 | g = FlowNetwork() |
| 170 | g.add_vertices(xrange(nVertices)) |
| 171 | for i in xrange(nEdges): |
| 172 | u,v,w = map(int, self.fd.next().split()) |
| 173 | if u < v: |
| 174 | g.add_edge(u, v, w) |
| 175 | else: |
| 176 | g.add_edge(v, u, w) |
| 177 | g.src, g.sink = 0, nVertices-1 |
| 178 | else: |
| 179 | g = UndirectedGraph() |
| 180 | g.add_vertices(xrange(nVertices)) |
| 181 | for i in xrange(nEdges): |
| 182 | u,v,w = map(int, self.fd.next().split()) |
| 183 | g.add_edge(u, v, w) |
| 184 | yield g |
| 185 | except StopIteration: |
| 186 | break |
| 187 | |
| 188 | def __del__(self): |
| 189 | self.fd.close() |
| 190 | |
| 191 | if __name__=='__main__': |
| 192 | import time |