Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Author: Pablo Moreno Olalla
- Email address: darthbrevu@yahoo.es
- */
- #include <set>
- #include <cstdio>
- #include <string>
- #include <vector>
- #include <fstream>
- #include <sstream>
- #include <iostream>
- #include <algorithm>
- using namespace std;
- //There are two basic options: either the contents of the file are stored in memory, or it is parsed
- //each time some data must be found.
- //Both approaches are quite inefficient in some sense (space/speed), so I choose the spatially inefficient
- //code, which is really easy! The program will still be a little heavy in terms of memory usage, which is
- //actually not that much of a deal. Several Mb are more than enough.
- class TaskData {
- public:
- unsigned long long myDuration;
- set<size_t> dependencies;
- long long totalDuration; //-1 if not set.
- };
- //I'll use a mix of C and C++ syntaxes. That's how I feel the most comfortable.
- bool readFile(const string &in,vector<TaskData> &out) {
- ifstream ifs(in.c_str());
- if (!ifs.is_open()) return false;
- string tmp;
- //Read the number of tasks
- unsigned long long N; //OK, 32 bits are enough for *this* case, but that won't *always* be true, right?
- do {
- getline(ifs,tmp);
- if (ifs.fail()||ifs.eof()) {
- ifs.close();
- return false;
- }
- } while (sscanf(tmp.c_str(),"%Lu",&N)!=1);
- out.resize(static_cast<size_t>(N));
- //Read the duration of each task.
- unsigned long long a,b;
- for (unsigned long long i=0;i<N;++i) {
- do {
- getline(ifs,tmp);
- if (ifs.fail()||ifs.eof()) {
- ifs.close();
- return false;
- }
- } while (sscanf(tmp.c_str(),"%Lu,%Lu",&a,&b)!=2);
- if (a!=i) {
- cout<<"Missing task "<<i<<'!'<<endl;
- ifs.close();
- return false;
- }
- out[i].myDuration=b;
- out[i].totalDuration=-1;
- }
- //Now read the dependencies.
- for (;;) {
- getline(ifs,tmp);
- if (ifs.eof()) {
- ifs.close();
- return true;
- } else if (ifs.fail()) {
- ifs.close();
- return false;
- }
- istringstream iss(tmp);
- iss>>a;
- if (iss.fail()) continue;
- for (;;) {
- iss.ignore(1); //The comma.
- iss>>b;
- if (iss.fail()) break;
- else out[a].dependencies.insert(b);
- }
- }
- }
- unsigned long long getDuration(vector<TaskData> &data,size_t i) {
- TaskData &td=data[i];
- if (td.totalDuration>=0) return static_cast<unsigned long long>(td.totalDuration);
- unsigned long long maxN=0;
- set<size_t>::const_iterator end=td.dependencies.end();
- for (set<size_t>::const_iterator it=td.dependencies.begin();it!=end;++it) maxN=max(maxN,getDuration(data,*it));
- return td.totalDuration=td.myDuration+maxN;
- }
- int main(int argc,char **argv) {
- string fn;
- fn=((argc<=1)?"in":string(argv[1]));
- //The following two lines are not strictly necessary, but make the code a little more handy.
- if (argc>=3) freopen(argv[2],"r",stdin);
- if (argc>=4) freopen(argv[3],"w",stdout);
- vector<TaskData> data(0);
- if (!readFile(fn,data)) return -1; //Couldn't read file.
- size_t i;
- cin>>i;
- if (!cin.fail()&&i<data.size()) cout<<i<<' '<<getDuration(data,i)<<endl;
- do {
- cin.ignore(1); //skip commas.
- cin>>i;
- if (!cin.fail()&&i<data.size()) cout<<i<<' '<<getDuration(data,i)<<endl;
- else break;
- } while (!cin.eof());
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement