QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#642500 | #4668. Find The Length | WRuperD | WA | 0ms | 3596kb | C++14 | 3.8kb | 2024-10-15 14:36:07 | 2024-10-15 14:36:09 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const long long inf = 1e18;
const int mininf = 1e9 + 7;
#define int long long
#define pb emplace_back
inline int read(){int x=0,f=1;char ch=getchar();while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}while(ch>='0'&&ch<='9'){x=(x<<1)+(x<<3)+(ch^48);ch=getchar();}return x*f;}
inline void write(int x){if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');}
#define put() putchar(' ')
#define endl puts("")
const int MAX = 305;
int n;
struct node{
int v, w;
};
int nxt[MAX][MAX];
int dis[MAX];
vector <node> g[MAX];
// int dfn[MAX], clk, low[MAX];
int dfn[MAX], low[MAX], clk;
bool bri[MAX][MAX];
void dfs(int u){
dfn[u] = low[u] = ++clk;
int cnt = 0;
for(auto v : g[u]){
if(!dfn[v.v]){
dfs(v.v);
low[u] = min(low[u], low[v.v]);
if(low[v.v] > dfn[u]){
cnt++;
bri[v.v][u] = bri[u][v.v] = 1;
// if(u != rt or cnt >= 2) cut[u] = 1;
}
}else low[u] = min(low[u], dfn[v.v]);
}
}
struct edge{
int from, val;
bool operator < (const edge & x) const{
return val > x.val;
}
};
void dji(int s){
priority_queue <edge> que;
for(int i = 1; i <= n; i++){
dis[i] = inf;
}
dis[s] = 0;
que.push(edge{s, 0});
while(!que.empty()){
auto u = que.top();
que.pop();
if(u.val != dis[u.from]) continue;
// for(int I = 1; I <= n; I++){
for(auto I : g[u.from]){
// int i = I;
int i = I.v;
if(nxt[u.from][i]){
if(dis[i] > dis[u.from] + nxt[i][u.from]){
dis[i] = dis[u.from] + nxt[i][u.from];
que.push(edge{i, dis[i]});
}
}
}
}
}
void solve(){
n = read();
for(int i = 1; i < n; i++){
for(int j = 1; j <= n; j++){
int w = read();
if(w == -1) continue;
nxt[i][j] = nxt[j][i] = w;
g[i].pb(node{j, w});
g[j].pb(node{i, w});
}
}
dfs(1);
// for(int i = 1; i <= n; i++) sort(g[i].begin(), g[i].end(), cmp);
for(int i = 1; i <= n; i++){
int ans = inf;
vector <pair<int,int>> To;
for(int j = 1; j <= n; j++){
if(nxt[i][j] and !bri[i][j]){
int tmp = nxt[i][j];
// nxt[i][j] = nxt[j][i] = 0;
// for(int k = 1; k <= n; k++){
// vis[k] = 0;
// }
// dfs(i);
// if(vis[j]){
To.pb(make_pair(tmp, j));
// }
// nxt[i][j] = nxt[j][i] = tmp;
}
}
sort(To.begin(), To.end());
// write(To.size()), endl;
for(int k = 0; k < min((long long)(To.size()), 25ll); k++){
int j = To[k].second;
int tmp = nxt[i][j];
nxt[i][j] = nxt[j][i] = 0;
dji(i);
ans = min(ans, dis[j] + tmp);
nxt[i][j] = nxt[j][i] = tmp;
}
// for(int k = To.size() - 1; k >= max((long long)(To.size() - 4), 0ll); k--){
//
// int j = To[k].second;
// int tmp = nxt[i][j];
// nxt[i][j] = nxt[j][i] = 0;
// dji(i);
// ans = min(ans, dis[j] + tmp);
// nxt[i][j] = nxt[j][i] = tmp;
// }
// for(int j = 1; j <= n; j++){
// if(nxt[i][j]){
// int tmp = nxt[i][j];
// nxt[i][j] = nxt[j][i] = 0;
// dji(i);
// ans = min(ans, dis[j] + tmp);
// nxt[i][j] = nxt[j][i] = tmp;
// }
// }
if(ans == inf) write(-1), endl;
else write(ans), endl;
}
endl;
// for(int i = 1; i <= n; i++){
// for(int j = 1; j <= n; j++) vis[j] = ins[j] = 0, dis[j] = 0 ,dis[j] = inf;
// ans = inf;
// rt = i;
// dis[i] = 0;
// dfs(i, i);
// // for(int j = 1; j <= n; j++){
// // write(dis[j]), put();
// // }
// // endl;
// // if(i == 2) write(dis[4]), endl;
// if(ans == inf) write(-1), put();
// else write(ans), put();
// }
// endl;
}
signed main(){
// freopen("road.in", "r", stdin);
// freopen("road.out", "w", stdout);
// int sub = read();
int t = 1;
while(t--) solve();
return 0;
}
詳細信息
Test #1:
score: 0
Wrong Answer
time: 0ms
memory: 3596kb
input:
1 5 0 0 3
output:
-1
result:
wrong answer read -1.000000000 but expected 43.982297150, error = 1.022736420