题目大意
思路
代码
#include<bits/stdc++.h>
using namespace std;
const int N = 1e4+7;
int n,m;
int a[N],b[N],c[N],d[N];
int h[N],e[N],ne[N],idx,st[N],cnt[N];
long double w[N],dist[N];
const double eps = 1e-8;
void add(int a, int b,long double c){
e[idx]=b,ne[idx]=h[a],w[idx]=c,h[a]=idx++;
}
bool check(long double k){
idx = 0;
for(int i= 1;i<=n;i++){
h[i]=-1;
cnt[i]=0;
st[i]=0;
dist[i]=0;
}
for(int i=1;i<=m;i++){
add(b[i],d[i],log(a[i])-log(c[i]*k));
}
queue<int> q;
for(int i=1;i<=n;i++){
q.push(i);
st[i]=1;
}
while(q.size()){
int t=q.front();
q.pop();
st[t]=0;
for(int i=h[t]; i!=-1; i=ne[i]){
int j=e[i];
if(dist[j]>dist[t]+w[i]+eps){
cnt[j]=cnt[t]+1;
dist[j]=dist[t]+w[i];
if(!st[j]){
q.push(j);
st[j]=1;
}
}
if(cnt[j]>=n) return true;
}
}
return false;
}
signed main(){
scanf("%d %d",&n,&m);
for(int i=1;i<=m;i++){
scanf("%d %d %d %d", &a[i],&b[i],&c[i],&d[i]);
}
long double l=0,r=2000;
int time=100;
while(time--){
double mid=(l+r)/2;
if(check(mid)) r=mid;
else l=mid;
}
printf("%.10lf\n", (double)l);
}
题目大意
思路
代码
#include <bits/stdc++.h>
struct O { int O = 0; } O;
using namespace std;
using namespace rel_ops;
using ll = long long;
using ld = long double;
template<typename T>
using pr_q_rev = priority_queue<T, vector<T>, greater<T>>;
using Pair = pair<ll, ll>;
ll gettime(const ll& m, multiset<Pair, greater<Pair>>& people) {
if (people.empty()) return 0;
ll ret = people.begin()->first;
if (people.size() <= m) {
people.clear();
return ret;
}
priority_queue<ll> q;
while (q.size() < m) {
q.push(people.begin()->second);
people.erase(people.begin());
}
while (1) {
ll qt = q.top();
q.pop();
auto it = people.lower_bound({qt, LLONG_MAX});
if (it == people.end()) break;
q.push(it->second);
people.erase(it);
}
return ret;
}
void solve(const int& T) {
ll n, m, k, ans = 0;
cin >> n >> m >> k;
multiset<Pair, greater<Pair>> up, down;
for (ll i = n; i > 0; i--) {
ll s, t;
cin >> s >> t;
if (s < t) up.insert({t, s});
else down.insert({s, t});
}
while (!(up.empty() && down.empty())) ans += max(gettime(m, up), gettime(m, down)) - 1;
cout << ans * 2 << '\n';
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
int t = 1;
for (int i = 1; i <= t; i++) solve(i);
return O.O;
}