POJ 1062 昂贵的聘礼
悲剧,poj讨论区所以的测试数据都测试通过了,提交后还是不正确
#include <iostream> #include <map> #include <vector> #include <set> using namespace std; struct Object { int ID; int Level; int Price; map<int,int> Replace; public: Object(int id,int level,int price):ID(id),Level(level),Price(price) { } }; struct PriceRoute { int Price; int Target; vector<int> Route; int Max; int Min; set<int> RemoveNode; }; vector<Object> vec; Object* pNode(int i) { return &vec[i-1]; } vector<PriceRoute> Target; bool ExistNode(int obj) { for (size_t i=0;i<Target.size();++i) { if (obj==Target[i].Target) { return true; } } return false; } PriceRoute* pRoute(int Obj) { for (size_t i=0;i<Target.size();++i) { if (Target[i].Target==Obj) { return &Target[i]; } } return NULL; } int main() { int m,n; cin>>m>>n; int price,level; int id,pri,num; for (int i=0;i<n;++i) { cin>>price>>level>>num; Object obj(i+1,level,price); for (int j=0;j<num;++j) { cin>>id>>pri; obj.Replace.insert(make_pair(id,pri)); } vec.push_back(obj); } PriceRoute pr; pr.Price=0; pr.Target=vec[0].ID; pr.Max=vec[0].Level; pr.Min=vec[0].Level; pr.Route.push_back(vec[0].ID); Target.push_back(pr); /*int test=0; cout<<"test"<<endl; cin>>test;*/ int i=0; while (i<n) { bool bReturn=false; map<pair<PriceRoute*,int>,int> NewRoute; for (vector<PriceRoute>::iterator iter=Target.begin();iter!=Target.end();++iter) { if (iter->Target==0) { continue; } int min=iter->Min; int max=iter->Max; int MaxTarget=0; int MaxValue=-100000; int objReturnValue=0; int objReturnNode=0; for (map<int,int>::iterator itObj=pNode(iter->Target)->Replace.begin(); itObj!=pNode(iter->Target)->Replace.end();++itObj) { if (ExistNode(itObj->first)) { continue; } if (iter->RemoveNode.find(itObj->first)!=iter->RemoveNode.end()) { continue; } if (abs(max-pNode(itObj->first)->Level)>m || abs(min-pNode(itObj->first)->Level)>m) { //需要把从差距最大的level开始回退节点 bReturn=true; objReturnValue=pNode(itObj->first)->Level; objReturnNode=itObj->first; continue; } int p1=pNode(iter->Target)->Price; int p2=itObj->second; int p3=pNode(itObj->first)->Price; if (p1-p2-p3>MaxValue) { MaxValue=p1-p2-p3; MaxTarget=itObj->first; } } if (MaxValue>-100000) { NewRoute.insert(make_pair(make_pair(&(*iter),MaxTarget),MaxValue)); } if (bReturn) { int MaxLevelNode=0; int NextNode=objReturnNode; for (int k=iter->Route.size()-1;k>=0;--k) { if (abs(pNode(iter->Route[k])->Level-objReturnValue)>m) { MaxLevelNode=iter->Route[k]; break; } PriceRoute* pRouteTarget=pRoute(iter->Route[k]); NextNode=iter->Route[k]; if (NULL!=pRouteTarget) { pRouteTarget->Target=0; --i; } } if (0!=MaxLevelNode) { PriceRoute* pRouteTarget=pRoute(MaxLevelNode); if (NULL!=pRouteTarget) { pRouteTarget->RemoveNode.insert(NextNode); } } } } map<pair<PriceRoute*,int>,int>::iterator MaxIter=NewRoute.end(); for (map<pair<PriceRoute*,int>,int>::iterator iter=NewRoute.begin();iter!=NewRoute.end();++iter) { if (MaxIter==NewRoute.end()) { MaxIter=iter; } else if (iter->second+iter->first.first->Price>MaxIter->second) { MaxIter=iter; } } if (MaxIter!=NewRoute.end()) { PriceRoute cpy(*(MaxIter->first.first)); cpy.Target=MaxIter->first.second; cpy.Route.push_back(MaxIter->first.second); cpy.Price+=MaxIter->second; if (cpy.Max<pNode(MaxIter->first.second)->Level) { cpy.Max=pNode(MaxIter->first.second)->Level; } if (cpy.Min>pNode(MaxIter->first.second)->Level) { cpy.Min=pNode(MaxIter->first.second)->Level; } cpy.RemoveNode.clear(); Target.push_back(cpy); i++; } else if (!bReturn) { break; } } int maxVal=0; for (int i=0;i<Target.size();++i) { if (Target[i].Price>maxVal) { maxVal=Target[i].Price; } } cout<<vec[0].Price-maxVal<<endl; return 0; }