| ... | ... |
@@ -22,193 +22,193 @@ |
| 22 | 22 |
/// |
| 23 | 23 |
/// This program solves various problems given in DIMACS format. |
| 24 | 24 |
/// |
| 25 | 25 |
/// See |
| 26 | 26 |
/// \verbatim |
| 27 | 27 |
/// dimacs-solver --help |
| 28 | 28 |
/// \endverbatim |
| 29 | 29 |
/// for more info on usage. |
| 30 | 30 |
/// |
| 31 | 31 |
|
| 32 | 32 |
#include <iostream> |
| 33 | 33 |
#include <fstream> |
| 34 | 34 |
#include <cstring> |
| 35 | 35 |
|
| 36 | 36 |
#include <lemon/smart_graph.h> |
| 37 | 37 |
#include <lemon/dimacs.h> |
| 38 | 38 |
#include <lemon/lgf_writer.h> |
| 39 | 39 |
#include <lemon/time_measure.h> |
| 40 | 40 |
|
| 41 | 41 |
#include <lemon/arg_parser.h> |
| 42 | 42 |
#include <lemon/error.h> |
| 43 | 43 |
|
| 44 | 44 |
#include <lemon/dijkstra.h> |
| 45 | 45 |
#include <lemon/preflow.h> |
| 46 | 46 |
#include <lemon/max_matching.h> |
| 47 | 47 |
|
| 48 | 48 |
using namespace lemon; |
| 49 | 49 |
typedef SmartDigraph Digraph; |
| 50 | 50 |
DIGRAPH_TYPEDEFS(Digraph); |
| 51 | 51 |
typedef SmartGraph Graph; |
| 52 | 52 |
|
| 53 | 53 |
template<class Value> |
| 54 | 54 |
void solve_sp(ArgParser &ap, std::istream &is, std::ostream &, |
| 55 | 55 |
DimacsDescriptor &desc) |
| 56 | 56 |
{
|
| 57 | 57 |
bool report = !ap.given("q");
|
| 58 | 58 |
Digraph g; |
| 59 | 59 |
Node s; |
| 60 | 60 |
Digraph::ArcMap<Value> len(g); |
| 61 | 61 |
Timer t; |
| 62 | 62 |
t.restart(); |
| 63 | 63 |
readDimacsSp(is, g, len, s, desc); |
| 64 | 64 |
if(report) std::cerr << "Read the file: " << t << '\n'; |
| 65 | 65 |
t.restart(); |
| 66 | 66 |
Dijkstra<Digraph, Digraph::ArcMap<Value> > dij(g,len); |
| 67 | 67 |
if(report) std::cerr << "Setup Dijkstra class: " << t << '\n'; |
| 68 | 68 |
t.restart(); |
| 69 | 69 |
dij.run(s); |
| 70 | 70 |
if(report) std::cerr << "Run Dijkstra: " << t << '\n'; |
| 71 | 71 |
} |
| 72 | 72 |
|
| 73 | 73 |
template<class Value> |
| 74 | 74 |
void solve_max(ArgParser &ap, std::istream &is, std::ostream &, |
| 75 | 75 |
Value infty, DimacsDescriptor &desc) |
| 76 | 76 |
{
|
| 77 | 77 |
bool report = !ap.given("q");
|
| 78 | 78 |
Digraph g; |
| 79 | 79 |
Node s,t; |
| 80 | 80 |
Digraph::ArcMap<Value> cap(g); |
| 81 | 81 |
Timer ti; |
| 82 | 82 |
ti.restart(); |
| 83 | 83 |
readDimacsMax(is, g, cap, s, t, infty, desc); |
| 84 | 84 |
if(report) std::cerr << "Read the file: " << ti << '\n'; |
| 85 | 85 |
ti.restart(); |
| 86 | 86 |
Preflow<Digraph, Digraph::ArcMap<Value> > pre(g,cap,s,t); |
| 87 | 87 |
if(report) std::cerr << "Setup Preflow class: " << ti << '\n'; |
| 88 | 88 |
ti.restart(); |
| 89 | 89 |
pre.run(); |
| 90 | 90 |
if(report) std::cerr << "Run Preflow: " << ti << '\n'; |
| 91 | 91 |
if(report) std::cerr << "\nMax flow value: " << pre.flowValue() << '\n'; |
| 92 | 92 |
} |
| 93 | 93 |
|
| 94 | 94 |
void solve_mat(ArgParser &ap, std::istream &is, std::ostream &, |
| 95 | 95 |
DimacsDescriptor &desc) |
| 96 | 96 |
{
|
| 97 | 97 |
bool report = !ap.given("q");
|
| 98 | 98 |
Graph g; |
| 99 | 99 |
Timer ti; |
| 100 | 100 |
ti.restart(); |
| 101 | 101 |
readDimacsMat(is, g, desc); |
| 102 | 102 |
if(report) std::cerr << "Read the file: " << ti << '\n'; |
| 103 | 103 |
ti.restart(); |
| 104 | 104 |
MaxMatching<Graph> mat(g); |
| 105 | 105 |
if(report) std::cerr << "Setup MaxMatching class: " << ti << '\n'; |
| 106 | 106 |
ti.restart(); |
| 107 | 107 |
mat.run(); |
| 108 | 108 |
if(report) std::cerr << "Run MaxMatching: " << ti << '\n'; |
| 109 | 109 |
if(report) std::cerr << "\nCardinality of max matching: " |
| 110 | 110 |
<< mat.matchingSize() << '\n'; |
| 111 | 111 |
} |
| 112 | 112 |
|
| 113 | 113 |
|
| 114 | 114 |
template<class Value> |
| 115 | 115 |
void solve(ArgParser &ap, std::istream &is, std::ostream &os, |
| 116 | 116 |
DimacsDescriptor &desc) |
| 117 | 117 |
{
|
| 118 |
std::stringstream iss(ap["infcap"]); |
|
| 118 |
std::stringstream iss(static_cast<std::string>(ap["infcap"])); |
|
| 119 | 119 |
Value infty; |
| 120 | 120 |
iss >> infty; |
| 121 | 121 |
if(iss.fail()) |
| 122 | 122 |
{
|
| 123 | 123 |
std::cerr << "Cannot interpret '" |
| 124 | 124 |
<< static_cast<std::string>(ap["infcap"]) << "' as infinite" |
| 125 | 125 |
<< std::endl; |
| 126 | 126 |
exit(1); |
| 127 | 127 |
} |
| 128 | 128 |
|
| 129 | 129 |
switch(desc.type) |
| 130 | 130 |
{
|
| 131 | 131 |
case DimacsDescriptor::MIN: |
| 132 | 132 |
std::cerr << |
| 133 | 133 |
"\n\n Sorry, the min. cost flow solver is not yet available.\n"; |
| 134 | 134 |
break; |
| 135 | 135 |
case DimacsDescriptor::MAX: |
| 136 | 136 |
solve_max<Value>(ap,is,os,infty,desc); |
| 137 | 137 |
break; |
| 138 | 138 |
case DimacsDescriptor::SP: |
| 139 | 139 |
solve_sp<Value>(ap,is,os,desc); |
| 140 | 140 |
break; |
| 141 | 141 |
case DimacsDescriptor::MAT: |
| 142 | 142 |
solve_mat(ap,is,os,desc); |
| 143 | 143 |
break; |
| 144 | 144 |
default: |
| 145 | 145 |
break; |
| 146 | 146 |
} |
| 147 | 147 |
} |
| 148 | 148 |
|
| 149 | 149 |
int main(int argc, const char *argv[]) {
|
| 150 | 150 |
typedef SmartDigraph Digraph; |
| 151 | 151 |
|
| 152 | 152 |
typedef Digraph::Arc Arc; |
| 153 | 153 |
|
| 154 | 154 |
std::string inputName; |
| 155 | 155 |
std::string outputName; |
| 156 | 156 |
|
| 157 | 157 |
ArgParser ap(argc, argv); |
| 158 | 158 |
ap.other("[INFILE [OUTFILE]]",
|
| 159 | 159 |
"If either the INFILE or OUTFILE file is missing the standard\n" |
| 160 | 160 |
" input/output will be used instead.") |
| 161 | 161 |
.boolOption("q", "Do not print any report")
|
| 162 | 162 |
.boolOption("int","Use 'int' for capacities, costs etc. (default)")
|
| 163 | 163 |
.optionGroup("datatype","int")
|
| 164 | 164 |
#ifdef HAVE_LONG_LONG |
| 165 | 165 |
.boolOption("long","Use 'long long' for capacities, costs etc.")
|
| 166 | 166 |
.optionGroup("datatype","long")
|
| 167 | 167 |
#endif |
| 168 | 168 |
.boolOption("double","Use 'double' for capacities, costs etc.")
|
| 169 | 169 |
.optionGroup("datatype","double")
|
| 170 | 170 |
.boolOption("ldouble","Use 'long double' for capacities, costs etc.")
|
| 171 | 171 |
.optionGroup("datatype","ldouble")
|
| 172 | 172 |
.onlyOneGroup("datatype")
|
| 173 | 173 |
.stringOption("infcap","Value used for 'very high' capacities","0")
|
| 174 | 174 |
.run(); |
| 175 | 175 |
|
| 176 | 176 |
std::ifstream input; |
| 177 | 177 |
std::ofstream output; |
| 178 | 178 |
|
| 179 | 179 |
switch(ap.files().size()) |
| 180 | 180 |
{
|
| 181 | 181 |
case 2: |
| 182 | 182 |
output.open(ap.files()[1].c_str()); |
| 183 | 183 |
if (!output) {
|
| 184 | 184 |
throw IoError("Cannot open the file for writing", ap.files()[1]);
|
| 185 | 185 |
} |
| 186 | 186 |
case 1: |
| 187 | 187 |
input.open(ap.files()[0].c_str()); |
| 188 | 188 |
if (!input) {
|
| 189 | 189 |
throw IoError("File cannot be found", ap.files()[0]);
|
| 190 | 190 |
} |
| 191 | 191 |
case 0: |
| 192 | 192 |
break; |
| 193 | 193 |
default: |
| 194 | 194 |
std::cerr << ap.commandName() << ": too many arguments\n"; |
| 195 | 195 |
return 1; |
| 196 | 196 |
} |
| 197 | 197 |
std::istream& is = (ap.files().size()<1 ? std::cin : input); |
| 198 | 198 |
std::ostream& os = (ap.files().size()<2 ? std::cout : output); |
| 199 | 199 |
|
| 200 | 200 |
DimacsDescriptor desc = dimacsType(is); |
| 201 | 201 |
|
| 202 | 202 |
if(!ap.given("q"))
|
| 203 | 203 |
{
|
| 204 | 204 |
std::cout << "Problem type: "; |
| 205 | 205 |
switch(desc.type) |
| 206 | 206 |
{
|
| 207 | 207 |
case DimacsDescriptor::MIN: |
| 208 | 208 |
std::cout << "min"; |
| 209 | 209 |
break; |
| 210 | 210 |
case DimacsDescriptor::MAX: |
| 211 | 211 |
std::cout << "max"; |
| 212 | 212 |
break; |
| 213 | 213 |
case DimacsDescriptor::SP: |
| 214 | 214 |
std::cout << "sp"; |
0 comments (0 inline)