QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#492971 | #171. Longest Shortest Path | Afterlife# | WA | 1ms | 8100kb | C++20 | 4.9kb | 2024-07-26 17:46:02 | 2024-07-26 17:46:03 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e5+1e3+7,M=1e6+1e3+7;
int cnt=1,head[N],hd[N];
struct Edge{
int ne,to,w;
}edg[M*2+1];
int n , m ,p , s , t;
int build(int u,int v,int w)
{
++cnt;
edg[cnt].to=v;
edg[cnt].ne=head[u];
edg[cnt].w=w;
head[u]=cnt;
++cnt;
edg[cnt].to=u;
edg[cnt].ne=head[v];
edg[cnt].w=0;
head[v]=cnt;
return cnt^1;
}
int d[N];
int S,T;//S=0, T=n+1
queue<int> q;
bool bfs()
{
fill(d+1,d+n+1,-1);
d[S]=0;
q.push(S);
while(!q.empty())
{
int x=q.front();
q.pop();
hd[x]=head[x];
for(int tmp=head[x];tmp;tmp=edg[tmp].ne)
{
int v=edg[tmp].to;
if(d[v]!=-1||!edg[tmp].w)
continue;
d[v]=d[x]+1;
q.push(v);
}
}
return d[T]!=-1;
}
int dinic(int x,int mn)
{
if(x==T||!mn)
return mn;
int flow=0,tmpf;
for(int &tmp=hd[x];tmp;tmp=edg[tmp].ne)
{
int v=edg[tmp].to;
if(d[v]==d[x]+1&&(tmpf=dinic(v,min(mn,edg[tmp].w)))>0)
{
flow+=tmpf;
mn-=tmpf;
edg[tmp].w-=tmpf;
edg[tmp^1].w+=tmpf;
}
if(!mn)
break;
}
return flow;
}
int flow()
{
int ret=0;
while(bfs())
ret+=dinic(S,INT_MAX);
return ret;
}
typedef pair<int,int> pii;
struct {
int dis[205];
Edge edg[M * 2 + 1] ;
int head[N];
int cnt;
int adde(int u,int v,int w) {
++cnt;
edg[cnt].to=v;
edg[cnt].ne=head[u];
edg[cnt].w=w;
head[u]=cnt;
return cnt;
}
void dij(int s) {
for(int i = 1;i <= n;i++) dis[i] = 1e9;
dis[s] = 0;
priority_queue<pii , vector<pii> , greater<pii> > pq;
pq.push({0 , s});
while(pq.size()) {
auto [w,u] = pq.top() ; pq.pop() ;
// printf("dis %d %d\n",u,w);
if(w != dis[u]) continue ;
// printf("%d %d\n",u,hd[u]);
// printf("dis %d %d\n",u,w);
for(int tmp=head[u];tmp;tmp=edg[tmp].ne) {
int v=edg[tmp].to;
// printf("edge %d %d : %d\n",u,v,edg[tmp].w);
if(dis[v] > dis[u] + edg[tmp].w) {
dis[v] = dis[u] + edg[tmp].w;
// printf("upd %d %d %d %d\n",u,v,dis[u],edg[tmp].w);
pq.push({dis[v] , v});
}
}
}
return ;
}
}D;
const int inf = 1e9;
int dd[2005] , c[2005];
pii E[2005];
bool in[2005];
int main()
{
ios::sync_with_stdio(false) ; cin.tie(0);
cin >> n >> m >> p >> s >> t;
S = s ; T = t;
for(int i = 1;i <= m;i++) {
int u , v ;cin >> u >> v >> dd[i] >> c[i];
// build(u , v , w);
D.adde(u , v , dd[i]);
in[i] = 0;
E[i] = {u , v};
}
int cost = 0;
D.dij(s) ;
double ans = D.dis[t];
int cur = 0;
while(cost < p) {
D.dij(s) ;
// printf("ok %d %d\n" , cost , p); exit(0) ;
// printf("%d %d\n" , D.dis[t] , cost);
int cdis = D.dis[t];
cnt = 1;
memset(head,0,sizeof(int) *(n + 10));
for(int i = 1 ; i <= m;i++) {
auto [u,v] = E[i];
if(D.dis[v] == D.dis[u] + dd[i]) {
build(u , v , c[i]);
in[i] = 1;
}
else in[i] = 0;
}
// for(int i = 1 ; i <= m;i++) {
// if(in[i]) continue ;
// auto [u,v] = E[i];
// if(D.dis[v] == D.dis[u] + dd[i]) {
// // printf("adde %d %d %d\n",u,v,c[i]);
// build(u , v , c[i]);
// in[i] = 1;
// }
// //build()
// }
cur = flow();
bfs() ;
// for(int i = 1;i <= n;i++) printf("D %d %d\n",i,d[i]);
for(int i = 1;i <= m;i++) {
auto [u,v] = E[i] ;
if((d[u] != -1 && d[v] == -1) && in[i]) {
// printf("cut %d %d\n",u,v);
D.edg[i].w = inf;
}
else D.edg[i].w = dd[i] ;
}
D.dij(s) ;
int upper = D.dis[t];
// printf("upper %d %d\n",upper , cdis) ;
if(cost + 1LL * cur * (upper - cdis) < p) {
cost += cur * (upper - cdis) ; ///every add upper - cdis
ans += (upper - cdis);
for(int i = 1;i <= m;i++) {
auto [u,v] = E[i];
if((d[u] != -1 && d[v] == -1) && in[i]) {
dd[i] += (upper - cdis) ;
}
D.edg[i].w = dd[i];
}
continue ;
}
else {ans += (double)(p - cost) / cur ; break;}
// exit(0);
}
printf("%.12lf\n" ,ans);
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 7836kb
input:
3 2 3 1 3 1 2 2 1 2 3 1 2
output:
6.000000000000
result:
ok found '6.0000000', expected '6.0000000', error '0.0000000'
Test #2:
score: 0
Accepted
time: 1ms
memory: 8064kb
input:
3 3 2 1 3 1 2 1 1 2 3 1 1 1 3 1 1
output:
2.500000000000
result:
ok found '2.5000000', expected '2.5000000', error '0.0000000'
Test #3:
score: 0
Accepted
time: 0ms
memory: 8064kb
input:
3 4 5 1 3 1 2 1 2 2 3 1 1 1 3 3 2 1 3 4 1
output:
4.250000000000
result:
ok found '4.2500000', expected '4.2500000', error '0.0000000'
Test #4:
score: 0
Accepted
time: 0ms
memory: 7848kb
input:
4 5 1 1 4 1 2 2 2 1 3 4 4 2 3 1 1 2 4 4 4 3 4 2 2
output:
6.000000000000
result:
ok found '6.0000000', expected '6.0000000', error '0.0000000'
Test #5:
score: -100
Wrong Answer
time: 0ms
memory: 8100kb
input:
4 5 2 1 4 1 2 2 2 1 3 4 4 2 3 1 1 2 4 4 4 3 4 2 2
output:
6.250000000000
result:
wrong answer 1st numbers differ - expected: '6.3333333', found: '6.2500000', error = '0.0131579'