Picnic Planning POJ - 1639最小限度生成树
5000+B,调死我了
set里判重居然是用符号比较判的,调出这个问题之后又莫名re,maxn改到3000居然才能跑discuss的样例
后来本机测发现弹了个bad_alloc。。看来以后做题vector不能随便开这么多了,估计改成静态数组每个一个tot就可以maxn30了
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 //#include<bits/stdc++.h> 2 //#pragma comment(linker, "/STACK:1024000000,1024000000") 3 #include<stdio.h> 4 #include<algorithm> 5 #include<queue> 6 #include<string.h> 7 #include<iostream> 8 #include<math.h> 9 #include<stack> 10 #include<set> 11 #include<map> 12 #include<vector> 13 #include<iomanip> 14 #include<bitset> 15 using namespace std; // 16 17 #define ll long long 18 #define ull unsigned long long 19 #define pb push_back 20 #define FOR(a) for(int i=1;i<=a;i++) 21 #define sqr(a) (a)*(a) 22 #define dis(a,b) sqrt(sqr(a.x-b.x)+sqr(a.y-b.y)) 23 ll qp(ll a,ll b,ll mod){ 24 ll t=1;while(b){if(b&1)t=t*a%mod;b>>=1;a=a*a%mod;}return t; 25 } 26 struct DOT{int x;int y;}; 27 inline void read(int &x){int k=0;char f=1;char c=getchar();for(;!isdigit(c);c=getchar())if(c=='-')f=-1;for(;isdigit(c);c=getchar())k=k*10+c-'0';x=k*f;} 28 void ex(){puts("No");exit(0);} 29 const int dx[4]={0,0,-1,1}; 30 const int dy[4]={1,-1,0,0}; 31 const int inf=0x3f3f3f3f; 32 const ll Linf=0x3f3f3f3f3f3f3f3fLL; 33 const ll Mod=1e18+7; 34 35 const int maxn=3300; 36 37 int n,lim; 38 map<string,int>idx; 39 char buf[2][15]; 40 int tot; 41 int edgcnt; 42 43 struct EDGE{ 44 int u,v,w; 45 bool del; 46 int id; 47 friend bool operator <(EDGE A,EDGE B){return A.w<B.w;} 48 }; 49 set<int>DES; 50 vector<EDGE>G[maxn]; 51 vector<EDGE>T[maxn]; //树图 52 53 int belong[maxn]; 54 vector<int>hav[maxn]; 55 int blkcnt; 56 int root; 57 58 int fa[maxn]; 59 void init(int n){for(int i=1;i<=n;i++)fa[i]=i;} 60 int find(int x){return fa[x]==x?x:fa[x]=find(fa[x]);} 61 void unite(int x,int y){x=find(x);y=find(y);fa[x]=y;} 62 63 vector<EDGE>BLK; 64 65 void dfs(int x,int fa){ 66 if(belong[x])return; 67 belong[x]=blkcnt; 68 hav[blkcnt].pb(x); 69 for(int i=0;i<G[x].size();i++){ 70 int v=G[x][i].v,w=G[x][i].w; 71 if(v==fa)continue; 72 if(v==root)continue; 73 dfs(v,x); 74 } 75 } 76 77 78 int ans; 79 80 set<int>SSS; //park连接的点 81 int du; //root的度数 82 83 struct NODE{ 84 EDGE del; 85 int val; 86 int to; 87 int newLen; // 88 friend bool operator<(NODE A,NODE B){ 89 return A.val<B.val; 90 } 91 }; 92 93 int flag; 94 95 void dfsx(int u,int fa,int &mxlen,EDGE &remo,int gf){ 96 //if(ans==82 && gf==6){ 97 // cout<<u<<"路径"<<T[6].size()<<endl; 98 //} 99 if(u==root){ 100 flag=1;return; 101 } 102 103 104 for(int i=0;i<T[u].size();i++){ 105 106 if(DES.count(T[u][i].id))continue; 107 108 //if(ans==82 && gf==6 && u==6){ 109 // cout<<"goto"<<T[u][i].v<<"ww"<<i<<endl; 110 //} 111 112 113 int v=T[u][i].v,w=T[u][i].w; 114 if(v==fa)continue; 115 116 int tmplen=mxlen; 117 EDGE tmpremo=remo; 118 119 if(mxlen<w){ 120 mxlen=w; 121 remo=T[u][i]; 122 } 123 124 dfsx(v,u,mxlen,remo,gf); 125 126 if(!flag){ 127 mxlen=tmplen; 128 remo=tmpremo; 129 }else{ 130 return; 131 } 132 } 133 } 134 135 int main(){ 136 while(~scanf("%d",&n)){ 137 138 int x; 139 lim=0; 140 tot=0;blkcnt=0;ans=0;du=0;edgcnt=0;root=0; 141 142 for(int i=0;i<=32;i++){ 143 G[i].clear();T[i].clear(); 144 hav[i].clear(); 145 } 146 du=0;BLK.clear();SSS.clear();DES.clear(); 147 memset(belong,0,sizeof belong);idx.clear(); 148 flag=0; 149 150 151 152 //idx["Park"]=8; 153 //idx["1"]=1;idx["2"]=2; 154 //idx["3"]=3;idx["4"]=4; 155 //idx["5"]=5;idx["6"]=6; 156 //idx["7"]=7;idx["8"]=8; 157 //tot=8; 158 159 160 161 for(int i=1;i<=n;i++){ 162 scanf("%s%s%d",buf[0]+1,buf[1]+1,&x); 163 164 165 if(idx[buf[0]+1]==0){ 166 idx[buf[0]+1]=++tot; 167 } 168 if(idx[buf[1]+1]==0){ 169 idx[buf[1]+1]=++tot; 170 } 171 172 173 int u=idx[buf[0]+1],v=idx[buf[1]+1]; 174 175 //cout<<u<<"ww"<<v<<endl; 176 177 int w=x; 178 179 G[idx[buf[0]+1]].pb((EDGE){u,v,w,false,edgcnt++}); 180 G[idx[buf[1]+1]].pb((EDGE){v,u,w,false,edgcnt++}); 181 } 182 scanf("%d",&lim); 183 184 root=idx["Park"]; 185 for(int i=1;i<=tot;i++){ 186 if(!belong[i] && i!=root){ 187 ++blkcnt; 188 dfs(i,i); 189 } 190 } 191 192 init(tot); 193 194 for(int i=1;i<=blkcnt;i++){ 195 int mnlen=inf,toid=0; 196 197 BLK.clear(); 198 for(int j=0;j<hav[i].size();j++){ 199 for(int k=0;k<G[hav[i][j]].size();k++){ 200 int v=G[hav[i][j]][k].v,w=G[hav[i][j]][k].w; 201 if(v!=root)BLK.pb(G[hav[i][j]][k]); 202 else{ 203 if(w<mnlen){ 204 mnlen=w; 205 toid=hav[i][j]; 206 } 207 } 208 } 209 } 210 211 sort(BLK.begin(),BLK.end()); 212 213 for(int j=0;j<BLK.size();j++){ 214 int x=BLK[j].u,y=BLK[j].v; 215 x=find(x);y=find(y); 216 if(x==y)continue; 217 else{ 218 unite(x,y); 219 ans+=BLK[j].w; 220 221 x=BLK[j].u; 222 y=BLK[j].v; 223 224 T[x].pb((EDGE){x,y,BLK[j].w,false,edgcnt++}); 225 T[y].pb((EDGE){y,x,BLK[j].w,false,edgcnt++}); 226 227 //cout<<x<<" "<<y<<" "<<BLK[j].w<<endl; 228 } 229 } 230 231 //cout<<toid<<"www"<<mnlen<<endl; 232 233 //cout<<ans<<"ww"<<endl;exit(0); 234 //选代表元素 235 T[root].pb((EDGE){root,toid,mnlen,false,edgcnt++}); 236 T[toid].pb((EDGE){toid,root,mnlen,false,edgcnt++}); 237 238 ans+=mnlen; 239 SSS.insert(toid); 240 du++; 241 } 242 243 while(du<lim){ 244 priority_queue<NODE>Q; 245 246 for(int i=0;i<G[root].size();i++){ 247 248 int v=G[root][i].v,w=G[root][i].w; 249 if(SSS.count(v))continue; 250 EDGE plc; 251 int vvl=0; 252 253 flag=0; 254 255 dfsx(v,v,vvl,plc,v); //v到root的路径找最长边 256 257 //if(ans==82){ 258 // cout<<v<<"ww"<<plc.u<<"__"<<plc.v<<endl; 259 //} 260 261 Q.push((NODE){plc,vvl-w,v,w}); 262 } 263 if(Q.empty())break; 264 NODE now=Q.top(); 265 if(now.val<0)break; 266 ans-=now.val; 267 268 //cout<<now.to<<"ww"<<now.newLen<<"ww"<<now.val<<"ww"<<ans<<endl; 269 //cout<<now.del.u<<"ww"<<now.del.v<<endl; 270 271 T[root].pb((EDGE){root,now.to,now.newLen,edgcnt++}); 272 T[now.to].pb((EDGE){now.to,root,now.newLen,edgcnt++}); 273 274 //cout<<du<<"du"<<now.to<<"qqq"<<root<<endl; 275 276 SSS.insert(now.to); 277 DES.insert(now.del.id); 278 DES.insert(now.del.id^1); 279 280 du++; 281 } 282 printf("Total miles driven: %d\n",ans); 283 } 284 }