QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#546728 | #4370. Road Times | PhantomThreshold | RE | 21ms | 35636kb | C++17 | 4.9kb | 2024-09-04 12:27:29 | 2024-09-04 12:27:29 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
using db = long double;
const db eps = 1e-8;
int sgn(db x) { return x < -eps ? -1 : x > eps; }
int cmp(db k1,db k2){return sgn(k1-k2);}
namespace LP {
const int N = 1005, M = 1005;
int n, m; // n : 变量个数,m : 约束个数
db a[M + N][N], x[N + M];
// 约束:对于 1 <= i <= m : a[i][0] + \sum_j x[j] * a[i][j] >= 0
// x[j] >= 0
// 最大化 \sum_j x[j] * a[0][j]
int id[N + M];
void pivot(int p, int o) {
std::swap(id[p], id[o + n]);
db w = -a[o][p];
for(int i = 0;i <= n;++i) a[o][i] /= w;
a[o][p] = -1 / w;
for(int i = 0;i <= m;++i) if(sgn(a[i][p]) && i != o) {
db w = a[i][p]; a[i][p] = 0;
for(int j = 0;j <= n;++j) a[i][j] += w * a[o][j];
}
}
db solve() { // nan : 无解,inf : 无界,否则返回最大值
for(int i = 1;i <= n + m;++i) id[i] = i;
for(;;) {
int p = 0, min = 1;
for(int i = 1;i <= m;++i) {
if(a[i][0] < a[min][0]) min = i;
}
if(a[min][0] >= -eps) break;
for(int i = 1;i <= n;++i) if(a[min][i] > eps && id[i] > id[p]) {
p = i;
}
if(!p) return nan("");
pivot(p, min);
}
for(;;) {
int p = 1;
for(int i = 1;i <= n;++i) if(a[0][i] > a[0][p]) p = i;
if(a[0][p] < eps) break;
db min = INFINITY; int o = 0;
for(int i = 1;i <= m;++i) if(a[i][p] < -eps) {
db w = -a[i][0] / a[i][p]; int d = sgn(w - min);
if(d < 0 || !d && id[i] > id[o]) o = i, min = w;
}
if(!o) return INFINITY;
pivot(p, o);
}
for(int i = 1;i <= m;++i) x[id[i + n]] = a[i][0];
return a[0][0];
}
}
const db INF=1e18;
db g[35][35];
int idcnt=0;
int id[35][35];
db dis[35][35];
int main(){
ios_base::sync_with_stdio(false);
cerr << fixed << setprecision(4);
cout << fixed << setprecision(12);
int n;
cin >> n;
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
int x;
cin >> x;
if (x!=-1) g[i][j]=x;
else g[i][j]=INF;
if (x>0) id[i][j]=++idcnt;
}
}
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
dis[i][j]=g[i][j];
}
}
for (int k=0;k<n;k++){
for (int i=0;i<n;i++){
for (int j=0;j<n;j++){
if (k==i || i==j || j==k) continue;
dis[i][j]=min(dis[i][j],dis[i][k]+dis[k][j]);
}
}
}
// for (int i=0;i<n;i++){
// for (int j=0;j<n;j++){
// cerr << dis[i][j] << " ";
// }
// cerr << endl;
// }
// for (int i=0;i<n;i++){
// for (int j=0;j<n;j++){
// cerr << id[i][j] << " ";
// }
// cerr << endl;
// }
vector<vector<db>> restrict;
for (int i=1;i<=idcnt;i++){
{
vector<db> tmp(idcnt+1);
tmp[0]=-1;
tmp[i]=1;
restrict.emplace_back(tmp);
}
{
vector<db> tmp(idcnt+1);
tmp[0]=2;
tmp[i]=-1;
restrict.emplace_back(tmp);
}
}
int r;
cin >> r;
for (int cc=1;cc<=r;cc++){
int st,ed;
db d;
cin >> st >> ed >> d;
vector<int> path;
for (int now=st;now!=ed;){
path.emplace_back(now);
int nxt=0;
for (int i=0;i<n;i++) if (i!=now && cmp(g[now][i]+dis[i][ed],dis[now][ed])==0) nxt=i;
now=nxt;
}
path.emplace_back(ed);
// for (auto x:path) cerr << x << "->";
// cerr << endl;
vector<db> tmp(idcnt+1);
tmp[0]=-d;
int path_sz=(int)path.size();
for (int i=1;i<path_sz;i++){
int u=path[i-1];
int v=path[i];
tmp[id[u][v]]=g[u][v];
}
restrict.emplace_back(tmp);
for (auto &x:tmp) x=x*(-1);
restrict.emplace_back(tmp);
}
// cerr << "--------restrict----------" << endl;
// for (auto &v:restrict){
// for (auto x:v) cerr << x << " ";
// cerr << endl;
// }
// cerr << "------------------" << endl;
int Q;
cin >> Q;
for (;Q--;){
int st,ed;
cin >> st >> ed;
vector<int> path;
for (int now=st;now!=ed;){
// cerr << "now : " << now << endl;
path.emplace_back(now);
int nxt=0;
for (int i=0;i<n;i++) if (i!=now && cmp(g[now][i]+dis[i][ed],dis[now][ed])==0) nxt=i;
now=nxt;
}
path.emplace_back(ed);
vector<db> tmp(idcnt+1);
int path_sz=(int)path.size();
for (int i=1;i<path_sz;i++){
int u=path[i-1];
int v=path[i];
tmp[id[u][v]]=g[u][v];
}
// for (auto x:path) cerr << x << "->";
// cerr << endl;
// for (auto x:tmp) cerr << x << " ";
// cerr << endl;
db ans1=0,ans2=0;
{
memset(LP::a,0,sizeof(LP::a));
memset(LP::x,0,sizeof(LP::x));
memset(LP::id,0,sizeof(LP::id));
LP::n=idcnt;
for (auto &vec:restrict){
++LP::m;
for (int i=0;i<=idcnt;i++) LP::a[LP::m][i]=vec[i];
}
for (int i=0;i<=idcnt;i++) LP::a[0][i]=tmp[i]*(-1);
ans1=LP::solve()*(-1);
}
{
memset(LP::a,0,sizeof(LP::a));
memset(LP::x,0,sizeof(LP::x));
memset(LP::id,0,sizeof(LP::id));
LP::n=idcnt;
for (auto &vec:restrict){
++LP::m;
for (int i=0;i<=idcnt;i++) LP::a[LP::m][i]=vec[i];
}
for (int i=0;i<=idcnt;i++) LP::a[0][i]=tmp[i];
ans2=LP::solve();
}
cout << st << " " << ed << " " << ans1 << " " << ans2 << "\n";
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 12ms
memory: 35528kb
input:
3 0 50 -1 55 0 40 -1 40 0 1 0 2 120 3 0 1 1 2 1 0
output:
0 1 50.000000000000 80.000000000000 1 2 40.000000000000 70.000000000000 1 0 55.000000000000 110.000000000000
result:
ok 12 numbers
Test #2:
score: 0
Accepted
time: 13ms
memory: 35500kb
input:
3 0 50 -1 55 0 40 -1 40 0 1 0 2 90 3 0 1 1 2 1 0
output:
0 1 50.000000000000 50.000000000000 1 2 40.000000000000 40.000000000000 1 0 55.000000000000 110.000000000000
result:
ok 12 numbers
Test #3:
score: 0
Accepted
time: 14ms
memory: 35528kb
input:
3 0 50 -1 55 0 40 -1 40 0 1 0 2 180 3 0 1 1 2 1 0
output:
0 1 100.000000000000 100.000000000000 1 2 80.000000000000 80.000000000000 1 0 55.000000000000 110.000000000000
result:
ok 12 numbers
Test #4:
score: 0
Accepted
time: 21ms
memory: 35408kb
input:
6 0 960 -1 -1 -1 -1 -1 0 -1 -1 1000 -1 -1 1000 0 -1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 1000 0 970 -1 -1 -1 -1 -1 0 3 0 3 5900 2 3 5800 2 5 5700 6 0 1 2 1 1 4 4 3 4 5 0 5
output:
0 1 1900.000000000000 1920.000000000000 2 1 1800.000000000000 1820.000000000000 1 4 1980.000000000000 2000.000000000000 4 3 1980.000000000000 2000.000000000000 4 5 1880.000000000000 1900.000000000000 0 5 5800.000000000000 5800.000000000000
result:
ok 24 numbers
Test #5:
score: 0
Accepted
time: 19ms
memory: 35636kb
input:
6 0 960 -1 -1 -1 -1 -1 0 -1 -1 1000 -1 -1 1000 0 -1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 1000 0 940 -1 -1 -1 -1 -1 0 3 0 3 5900 2 3 5800 2 5 5700 6 0 1 2 1 1 4 4 3 4 5 0 5
output:
0 1 1920.000000000000 1920.000000000000 2 1 1820.000000000000 1820.000000000000 1 4 2000.000000000000 2000.000000000000 4 3 1980.000000000000 1980.000000000000 4 5 1880.000000000000 1880.000000000000 0 5 5800.000000000000 5800.000000000000
result:
ok 24 numbers
Test #6:
score: 0
Accepted
time: 20ms
memory: 35404kb
input:
6 0 950 -1 -1 -1 -1 -1 0 -1 -1 1000 -1 -1 1000 0 -1 -1 -1 -1 -1 -1 0 -1 -1 -1 -1 -1 1000 0 970 -1 -1 -1 -1 -1 0 3 0 3 5900 2 3 5800 2 5 5700 6 0 1 2 1 1 4 4 3 4 5 0 5
output:
0 1 1900.000000000000 1900.000000000000 2 1 1800.000000000000 1800.000000000000 1 4 2000.000000000000 2000.000000000000 4 3 2000.000000000000 2000.000000000000 4 5 1900.000000000000 1900.000000000000 0 5 5800.000000000000 5800.000000000000
result:
ok 24 numbers
Test #7:
score: -100
Runtime Error
input:
10 0 123 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 234 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 345 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 456 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 567 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 678 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 890 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 901 -1 -1 -1 -1 -1 -1 -1 -1 -1 0 555 666 -1 -1 -1 -1 -1 -1 -1 -1...
output:
0 0 0.000000000000 0.000000000000 0 1 216.000000000000 246.000000000000 0 2 450.000000000000 714.000000000000 0 3 1084.000000000000 1114.000000000000 0 4 1540.000000000000 1570.000000000000 0 5 2674.000000000000 2704.000000000000 0 6 3408.000000000000 3438.000000000000 0 7 4298.000000000000 4358.000...