QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#276841 | #7883. Takeout Delivering | kokomiisbest | WA | 233ms | 31712kb | C++17 | 3.3kb | 2023-12-06 11:34:26 | 2023-12-06 11:34:26 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
int n,m;
const int N = 3e5+10,M = 1e6+10;
int h[N],e[M],ne[M],w[M],idx;
int max1[N],max2[N],dist[N];//dist?\xbb\xa4?\xc3\xe6·\xbe\xb6\xb5\xc4\xd7\xeeС?\xbe\xb1·
int dist1[N];
int ans[N];
bool st[N];
void add(int a,int b,int c){
w[idx]=c;
e[idx]=b;
ne[idx]=h[a];
h[a]=idx++;
}
//并查集
int pre[N];
void init(){
for(int i=1;i<=n;i++) pre[i]=i;
}
int find(int x){
if(pre[x]!=x) pre[x]=find(pre[x]);
return pre[x];
}
bool merge(int x,int y){
int px=find(x),py=find(y);
if(px!=py){
pre[px]=py;
return 1;
}
return 0;
}
typedef pair<int, int> PII;
void dij1(){//\xc7\xf3\xd7\xee\xb6\xcc?\xbe\xb1·
memset(dist,0x3f,sizeof dist);
memset(st,false,sizeof st);
dist[1]=0;
// st[1]=true;
priority_queue<PII, vector<PII>, greater<PII>> pq;
pq.push({0,1});
while(pq.size()){
PII k=pq.top();
pq.pop();
int ver=k.second,distance=k.first;
if(st[ver]) continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i]){
int j=e[i];
// if(w[i]>dist[j]){
// dist[j]=w[i];
// cout<<j<<" "<<dist[j]<<endl;
// pq.push({dist[j],j});
// }
int val=max(dist[ver],w[i]);
// cout<<j<<" "<<val<<endl;
if(val<dist[j]){
dist[j]=val;
// cout<<j<<" "<<dist[j]<<endl;
pq.push({dist[j],j});
}
}
}
}
void dij2(){
memset(dist1,0x3f,sizeof dist1);
memset(st,false,sizeof st);
dist1[n]=0;
priority_queue<PII, vector<PII>, greater<PII>> pq;
pq.push({0,n});
while(pq.size()){
PII k=pq.top();
pq.pop();
int ver=k.second,distance=k.first;
if(st[ver]) continue;
st[ver]=true;
for(int i=h[ver];i!=-1;i=ne[i]){
int j=e[i];
// if(w[i]>dist[j]){
// dist[j]=w[i];
// cout<<j<<" "<<dist[j]<<endl;
// pq.push({dist[j],j});
// }
int val=max(dist1[ver],w[i]);
// cout<<j<<" "<<val<<endl;
if(val<dist1[j]){
dist1[j]=val;
// cout<<j<<" "<<dist[j]<<endl;
pq.push({dist1[j],j});
}
}
}
}
struct edge{
int a,b,c;
};
vector<edge> MST;
vector<edge> e1;
int main(){
memset(h,-1,sizeof h);
cin>>n>>m;
int ans1=0x3f3f3f3f;
while(m--){
int a,b,c;
cin>>a>>b>>c;
e1.push_back({a,b,c});
if((a==1&&b==n)||(a==n&&b==1)){
ans1=c;
}
add(a,b,c),add(b,a,c);
}
//最大生成树
sort(e1.begin(),e1.end(),[&](edge x,edge y){
return x.c>y.c;
});
init();
for(auto x:e1){
if(merge(x.a,x.b)){
MST.push_back(x);
}
}
// cout<<"--"<<ans1<<endl;
dij1();
dij2();
int ans=0x3f3f3f3f;
// for(int i=2;i<=n-1;i++){
// // cout<<dist[i]<<"---"<<dist1[i]<<endl;
// ans=max(ans,(ll)dist[i]+(ll)dist1[i]);
// }
for(auto x:MST){
int a=x.a,b=x.b,c=x.c;
if(dist1[a]<=c&&dist[b]<=c) ans=min(ans,c+max(dist1[a],dist[b]));
if(dist1[b]<=c&&dist[a]<=c) ans=min(ans,c+max(dist1[b],dist[a]));
}
ans1=min(ans1,ans);
cout<<ans1<<endl;
// cout<<ans<<" "<<ans1<<endl;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 15028kb
input:
4 6 1 2 2 1 3 4 1 4 7 2 3 1 2 4 3 3 4 9
output:
5
result:
ok 1 number(s): "5"
Test #2:
score: -100
Wrong Answer
time: 233ms
memory: 31712kb
input:
300000 299999 80516 80517 597830404 110190 110191 82173886 218008 218009 954561262 250110 250111 942489774 66540 66541 156425292 34947 34948 239499776 273789 273790 453201232 84428 84429 439418398 98599 98600 326095035 55636 55637 355015760 158611 158612 684292473 43331 43332 43265001 171621 171622 ...
output:
1061109567
result:
wrong answer 1st numbers differ - expected: '1999991697', found: '1061109567'