QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#451501 | #8304. Key Project | -Ofast | TL | 301ms | 25784kb | C++98 | 3.2kb | 2024-06-23 15:20:17 | 2024-06-23 15:20:20 |
Judging History
answer
#include <bits/stdc++.h>
#define ll long long
using namespace std;
const int N=5e4+10;
vector <int> P[N],Q[N];
int n,m,dis[N],a[N],b[N],c[N],d[N];
namespace MCMF{
const int N=3e5+10;
int n,s,t,head[N],from[N],to[N],nxt[N],lst[N];
int f[N],tmphead[N],c[N],cnt=-1;
ll maxflow,mincost;
void add(int u,int v,int fl,int w){
++cnt;
if(head[u]!=-1)lst[head[u]]=cnt;
nxt[cnt]=head[u];from[cnt]=u;
head[u]=cnt;to[cnt]=v;
f[cnt]=fl;c[cnt]=w;
++cnt;
if(head[v]!=-1)lst[head[v]]=cnt;
nxt[cnt]=head[v];from[cnt]=v;
head[v]=cnt;to[cnt]=u;
f[cnt]=0;c[cnt]=-w;
}
void del(int id){
if(nxt[id]!=-1){
if(lst[id]!=-1){
lst[nxt[id]]=lst[id];
nxt[lst[id]]=nxt[id];
}
else head[from[id]]=nxt[id],lst[nxt[id]]=-1;
}else{
if(lst[id]!=-1)nxt[lst[id]]=-1;
else head[from[id]]=-1;
}
}
ll dis[N];int vis[N],pre[N][2];
struct Queue{
int head,tail,a[N];
void push(int x){a[++tail]=x;}
void pop(){++head;}
int size(){return tail-head+1;}
int front(){return a[head];}
}q;
void SPFA(){
q.head=q.tail=0;
for(int i=0;i<=n;i++)dis[i]=1e18,vis[i]=0;
q.push(s);vis[s]=1;dis[s]=0;
while(q.size()){
int u=q.front();q.pop();
vis[u]=0;
for(int i=head[u];~i;i=nxt[i]){
int v=to[i],w=c[i];
if(!f[i])continue;
if(dis[v]>dis[u]+w){
dis[v]=dis[u]+w;
pre[v][0]=u;
pre[v][1]=i;
if(!vis[v]){
q.push(v);vis[v]=1;
}
}
}
}
}
int pid[N],qid[N],path[N];
void solve(){
while(true){
SPFA();
if(dis[t]==1e18)break;
ll minf=1e9,sumc=0;int now=t;
int psz=0;
while(now!=s){
path[psz++]=pre[now][1];
minf=min(minf,1ll*f[pre[now][1]]);
sumc+=c[pre[now][1]];
now=pre[now][0];
}
for(int i=0;i<psz;i++){
f[path[i]]-=minf;
f[path[i]^1]+=minf;
}
for(int i=1;i<=::n;i++){
if(P[i].size()&&f[pid[i]]){
del(pid[i]);del(pid[i]^1);
P[i].pop_back();
if(P[i].size()){
add(s,i,1,P[i].back()),pid[i]=cnt;
}
}
if(Q[i].size()&&f[qid[i]]){
del(qid[i]);del(qid[i]^1);
Q[i].pop_back();
if(Q[i].size()){
add(i,t,1,Q[i].back()),qid[i]=cnt;
}
}
}
mincost+=1ll*minf*sumc;
cout<<mincost<<"\n";
}
}
}
int main(){
ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
memset(MCMF::head,-1,sizeof(MCMF::head));
memset(MCMF::lst,-1,sizeof(MCMF::lst));
cin>>n>>m;
for(int i=1;i<n;i++)cin>>dis[i];
for(int i=1;i<=m;i++)cin>>a[i]>>b[i];
for(int i=1;i<=m;i++)cin>>c[i]>>d[i];
MCMF::t=n+1;MCMF::s=0;MCMF::n=MCMF::t;
for(int i=1;i<n;i++){
MCMF::add(i,i+1,1e9,dis[i]);
MCMF::add(i+1,i,1e9,dis[i]);
}
for(int i=1;i<=m;i++)
Q[a[i]].push_back(b[i]);
for(int i=1;i<=m;i++)
P[c[i]].push_back(d[i]);
for(int i=1;i<=n;i++){
sort(Q[i].begin(),Q[i].end());
sort(P[i].begin(),P[i].end());
reverse(Q[i].begin(),Q[i].end());
reverse(P[i].begin(),P[i].end());
}
for(int i=1;i<=n;i++){
if(P[i].size())MCMF::add(MCMF::s,i,1,P[i].back()),MCMF::pid[i]=MCMF::cnt;
if(Q[i].size())MCMF::add(i,MCMF::t,1,Q[i].back()),MCMF::qid[i]=MCMF::cnt;
}
MCMF::solve();
//cerr<<1.0*clock()/CLOCKS_PER_SEC<<endl;
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 24880kb
input:
4 3 1 1 1 1 1 1 2 4 6 2 1 2 2 3 5
output:
3 8 20
result:
ok 3 lines
Test #2:
score: 0
Accepted
time: 0ms
memory: 24956kb
input:
50 500 118837 951904 671499 484461 639888 359339 502649 627559 5961 20812 433609 905677 200996 201161 615383 263424 151528 844324 472585 154315 741222 247963 722818 858419 386517 100125 931060 752896 865319 23063 949988 220229 897126 259228 583375 813279 112033 676073 925357 53124 394997 392593 4992...
output:
249916 1627724 4297306 7521922 11058813 14819098 18595354 22833314 27397118 32192806 38169968 44605698 51210995 57983422 65947214 73960441 82121445 90336822 99546401 109165243 119438147 129792015 140327562 150872437 161950777 173380273 185460073 198299711 211179357 224215634 237469142 251993186 2668...
result:
ok 500 lines
Test #3:
score: 0
Accepted
time: 30ms
memory: 24684kb
input:
100 5000 161024 333083 427714 106026 344162 443967 300826 786012 190136 836472 739686 718645 621258 379491 896890 853718 132289 761595 425423 238212 266456 878908 987607 383196 425157 464423 910874 113988 596436 427196 677260 53939 353039 610651 203820 412321 810742 75711 522237 749361 213154 313223...
output:
244596 673367 1114774 1653086 2235799 2859280 3550743 4328294 5111265 5970416 6833438 7698546 8596718 9537334 10673653 11841335 13117454 14432273 15830547 17278727 18734845 20229311 21782032 23362360 24997850 26692528 28391424 30147387 31907288 33748546 35691188 37645806 39609940 41609598 43629263 4...
result:
ok 5000 lines
Test #4:
score: 0
Accepted
time: 301ms
memory: 25784kb
input:
300 20000 589233 424913 26472 555010 877553 764101 539779 342418 861724 439040 459313 518843 421817 463969 416326 348384 802183 187604 666745 329607 31379 864405 383948 216216 214607 823562 806785 481248 308705 60717 10231 892503 634831 778333 229345 254773 810990 139390 203897 878093 12605 556113 7...
output:
100488 213140 349180 573842 815426 1116056 1462744 1812722 2244746 2703883 3174800 3651102 4147393 4650694 5158675 5668646 6183335 6716217 7283456 7888069 8500618 9122205 9750795 10400551 11050357 11710647 12415089 13120968 13843813 14567012 15300273 16039492 16782040 17525930 18274542 19025483 1979...
result:
ok 20000 lines
Test #5:
score: -100
Time Limit Exceeded
input:
500 50000 389931 323526 397980 127878 810602 175326 183833 681018 182880 117266 603325 622561 639554 107960 301053 423699 585725 728312 759747 401710 708795 716319 677907 222410 106480 693183 12062 99078 319397 27265 184818 547305 591605 673437 248572 838416 783112 514511 590199 304268 743695 152614...
output:
28948 81176 142183 274273 412436 564080 723576 907555 1106837 1312014 1542499 1776945 2029643 2293083 2564408 2845238 3127258 3429326 3734724 4077784 4422610 4769373 5120211 5471271 5829254 6193440 6559204 6925157 7299056 7676687 8055873 8443295 8835706 9228277 9633028 10040983 10449860 10867427 112...