HDU 6982(Road Discount-wqs二分)
Posted nike0good
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU 6982(Road Discount-wqs二分)相关的知识,希望对你有一定的参考价值。
给你一个无向图
n
n
n个点
m
m
m条边,每个边有一个代价
c
i
c_i
ci以及折扣价
d
i
d_i
di,现在要求对每个
k
(
0
≤
k
≤
n
−
1
)
k(0\\le k \\le n-1)
k(0≤k≤n−1),求恰取
k
k
k条折扣价的最小生成树的代价是多少。
n
≤
1
e
3
,
m
≤
2
e
5
,
c
i
,
d
i
≤
1
e
3
n\\le 1e3,m\\le2e5,c_i,d_i\\le 1e3
n≤1e3,m≤2e5,ci,di≤1e3
#include<bits/stdc++.h>
using namespace std;
#define For(i,n) for(int i=1;i<=n;i++)
#define Fork(i,k,n) for(int i=k;i<=n;i++)
#define ForkD(i,k,n) for(int i=n;i>=k;i--)
#define Rep(i,n) for(int i=0;i<n;i++)
#define ForD(i,n) for(int i=n;i;i--)
#define RepD(i,n) for(int i=n;i>=0;i--)
#define Forp(x) for(int p=pre[x];p;p=next[p])
#define Forpiter(x) for(int &p=iter[x];p;p=next[p])
#define Lson (o<<1)
#define Rson ((o<<1)+1)
#define MEM(a) memset(a,0,sizeof(a));
#define MEMI(a) memset(a,0x3f,sizeof(a));
#define MEMi(a) memset(a,128,sizeof(a));
#define MEMx(a,b) memset(a,b,sizeof(a));
#define INF (0x3f3f3f3f)
#define F (1000000007)
#define pb push_back
#define mp make_pair
#define fi first
#define se second
#define vi vector<int>
#define pi pair<int,int>
#define SI(a) ((a).size())
#define Pr(kcase,ans) printf("Case #%d: %lld\\n",kcase,ans);
#define PRi(a,n) For(i,n-1) cout<<a[i]<<' '; cout<<a[n]<<endl;
#define PRi2D(a,n,m) For(i,n) \\
For(j,m-1) cout<<a[i][j]<<' ';\\
cout<<a[i][m]<<endl; \\
#pragma comment(linker, "/STACK:102400000,102400000")
#define ALL(x) (x).begin(),(x).end()
#define gmax(a,b) a=max(a,b);
#define gmin(a,b) a=min(a,b);
#define sqr(x) (x*x)
typedef long long ll;
typedef long double ld;
typedef unsigned long long ull;
ll mul(ll a,ll b)return (a*b)%F;
ll add(ll a,ll b)return (a+b)%F;
ll sub(ll a,ll b)return ((a-b)%F+F)%F;
void upd(ll &a,ll b)a=(a%F+b%F)%F;
inline int read()
int x=0,f=1; char ch=getchar();
while(!isdigit(ch)) if (ch=='-') f=-1; ch=getchar();
while(isdigit(ch)) x=x*10+ch-'0'; ch=getchar();
return x*f;
#define MAXN (400000+10)
int n, m, u[MAXN],v[MAXN],id[MAXN],w[MAXN],d[MAXN],c[MAXN];
bool b[MAXN<<1];
int iid[MAXN],t0=0;
class bingchaji
public:
#define MAXN (1010)
int father[MAXN],n,cnt;
void mem(int _n)
n=cnt=_n;
For(i,n) father[i]=i;
int getfather(int x)
if (father[x]==x) return x;
return father[x]=getfather(father[x]);
void unite(int x,int y)
x=getfather(x);
y=getfather(y);
if (x^y)
--cnt;
father[x]=y;
bool same(int x,int y)
return getfather(x)==getfather(y);
S;
bool cmp(int i,int j)
return w[i]<w[j]||w[i]==w[j]&&i<j;
int an[1010];
int M;
int work(int m,bool flag,int &p1)
if(!flag)
For(i,m) id[i]=i;
sort(id+1,id+1+m,cmp);
S.mem(n);
int ans=0;
For(p,m)
int i=id[p];
if(!S.same(u[i],v[i]))
S.unite(u[i],v[i]);
p1+=i<=M;
if(!flag)b[i]=1;
ans+=w[i];
return ans;
int main()
// freopen("j.in","r",stdin);
// freopen(".out","w",stdout);
int T=read();
For(kcase,T)
n=read(),m=read();
For(i,m) u[i]=read(),v[i]=read(),c[i]=read(),d[i]=read(),b[i]=0;
int p1=0;
Rep(i,n) an[i]=INF;
M=m;
t0=0;
For(i,m) w[i]=c[i];
an[n-1]=work(m,0,p1); // no discount
For(i,m) w[i]=d[i];
an[0]=work(m,0,p1);
M=0;
For(i,m) if(b[i])
++M;
u[M]=u[i],v[M]=v[i];
d[M]=d[i],c[M]=c[i];
m=M;
For(i,M) u[i+M]=u[i],v[i+M]=v[i],w[i]=c[i],w[i+M]=d[i];
For(i,M) iid[i]=i; sort(iid+1,iid+1+M,cmp);
For(i,M) iid[i+M]=i+M; sort(iid+M+1,iid+1+2*M,cmp);
int pr=0;
Rep(mid,1002)
For(i,m) w[i]=c[i],w[i+m]=d[i]+mid;
int p1=1,p2=1;
For(ttt,2*M)
if(p1>M) id[ttt]=iid[p2+M],++p2;
else if(p2>M) id[ttt]=iid[p1],++p1;
else
if(w[iid[p1]]>w[iid[p2+M]]) id[ttt]=iid[p2+M],++p2;
else id[ttt]=iid[p1],++p1;
p1=0;
int ans=work(2*M,1,p1);
ans-mid*(n-1-p1);
gmin(an[p1],ans-mid*(n-1-p1))
if(pr+1<p1)
Fork(j,pr+1,p1-1) gmin(an[j],ans-mid*(n-1-j))
pr=p1;
RepD(i,n-2)
gmin(an[i],an[i+1])
RepD(i,n-1) printf("%d\\n",an[i]);
return 0;
以上是关于HDU 6982(Road Discount-wqs二分)的主要内容,如果未能解决你的问题,请参考以下文章
hdu1596find the safest road(floyd)
HDU 1598 find the most comfortable road