22/7/15

1,cf Productive Meeting;2,acwing 1134.最短路计数(模板题);3,acwing 1142.繁忙的都市(最小生成树);4,acwing 1143.联络员(kruskal);


1,cf Productive Meeting

题意:

思路:看样例,

 很容易想到一个做法是每次选最大值和次大值,答案增加一个次大值,然后接着选;所以维护最大值和次大值,由此想到大顶堆;这样一直取;

但很不幸,wa了;错误的原因就在最大值和次大值的更替上,每次取出来最大值和次大值,不能直接增加一个次大值,只增加一次,即将最大值和次大值减一后再放到堆里,这样就能实时保证最大值和次大值的更新;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<pair<int,int>,pair<int,int>> PIIII;
typedef double dob;
const int N=2e5+10;
int n;
void solve()
{
    cin>>n;
    pro_q<PII>heap;
    rep2(i,1,n)
    {
        int cs;cin>>cs;
        if(cs)heap.push({cs,i});
    }
    int num=0;
    vector<PII>ans;
    while(heap.size())
    {
        auto max1=heap.top();
        heap.pop();
        if(!heap.size())break;
        auto max2=heap.top();
        heap.pop();
        int maxx1=max1.first,maxx2=max2.first;
        num++;
        maxx1--;
        maxx2--;
        ans.pb({max1.second,max2.second});
        if(maxx1)heap.push({maxx1,max1.second});
        if(maxx2)heap.push({maxx2,max2.second});
    }
    cout<<num<<endl;
    int siz=ans.size();
    rep1(i,0,siz)cout<<ans[i].first<<" "<<ans[i].second<<endl;
}
signed main()
{
    quick_cin();
    T_solve();
    return 0;
}

2,最短路计数;

问1号点到其他点的最短路各有几条;

正常的最短路算法即可跑出1号点到其他路的最短路,求条数只需要中间加个条数的状态转移即可;

dist[j] > dist[t] +w[i] :到j的最短路条数等于到t的最短路条数;(因为j是选过来的一个最短路点,必定从t到j走一个,所以j只能由t走来)

dist[j] = dist[t] +w[i] : 到j的最短路条数需要加上到t的最短路条数,因为到j到t都可以,所以到两个点的最短路条数都算;

(这里理解下为什么这里是无向图用bfs且不用st判重,因为是往下一层更新距离的,而入队的条件只有距离还没被更新的时候,所以一旦距离确定了,肯定不会重复入队,所以这俩dist数组兼具st数组作用);

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<pair<int,int>,pair<int,int>> PIIII;
typedef double dob;
const int N=2e5+10;
const int mod=1e5+3;
tulun;
int n,m;
int cnt[N];
int dist[N],st[N];
void bfs()
{
    cnt[1]=1;
    queue<int>q;
    q.push(1);
    memset(dist,0x3f,dist);
    dist[1]=0;
    st[1]=1;
    while(q.size())
    {
        int t=q.front();
        q.pop();
        for(int i=h[t];~i;i=ne[i])
        {
            int j=e[i];
            if(dist[j]>dist[t]+1)
            {
                dist[j]=dist[t]+1;
                q.push(j);
                cnt[j]=cnt[t];
            }
            else if(dist[j]==dist[t]+1)
            {
                cnt[j]=(cnt[j]+cnt[t])%mod;
            }
        }
    }
}
signed main()
{
    quick_cin();
    cin>>n>>m;
    memset(h,-1,h);
    while(m--)
    {
        int a,b;
        cin>>a>>b;
        add2(a,b);add2(b,a);
    }
    bfs();
    rep2(i,1,n)cout<<cnt[i]<<endl;
    return 0;
}

3,繁忙的都市;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<pair<int,int>,pair<int,int>> PIIII;
typedef double dob;
const int N=2e5+10;
const int mod=1e5+3;
struct node
{
    int a,b,w;
    bool operator <(const node &b)
    {
        return w<b.w;
    }
}edge[N];
int n,m;
int p[N];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int ans,cnt;
void kruskal()
{
    rep2(i,1,n)p[i]=i;
    sort(edge+1,edge+m+1);
    rep2(i,1,m)
    {
        int a=edge[i].a,b=edge[i].b,w=edge[i].w;
        a=find(a);
        b=find(b);
        if(a!=b)
        {
            ans=max(ans,w);
            cnt++;
            p[a]=b;
        }
        if(cnt==n-1)return;
    }
}
signed main()
{
    quick_cin();
    cin>>n>>m;
    rep2(i,1,m)
    {
        int a,b,w;
        cin>>a>>b>>w;
        edge[i]={a,b,w};
    }
    kruskal();
    cout<<cnt<<' '<<ans;
    return 0;
}

这三个条件明显的就是让你搞个最小生成树,然后找出选取的边的最大值;模板题;

4,联络员;

考虑必须边的特殊情况即可;

#pragma GCC optimize(2)
#include<bits/stdc++.h>
#define rep1(i,a,n) for(register int i=(a);i<(n);++i) 
#define rep2(i,a,n) for(register int i=(a);i<=(n);++i) 
#define per1(i,n,a) for(register int i=(n);i>(a);i--) 
#define per2(i,n,a) for(register int i=(n);i>=(a);i--)
#define quick_cin() cin.tie(0),cout.tie(0),ios::sync_with_stdio(false)
#define memset(a,i,b) memset((a),(i),sizeof (b))
#define memcpy(a,i,b) memcpy((a),(i),sizeof (b))
#define pro_q priority_queue
#define pb push_back
#define pf push_front
#define endl "\n"
#define lowbit(m) ((-m)&(m))
#define YES cout<<"YES\n"
#define NO cout<<"NO\n"
#define Yes cout<<"Yes\n"
#define No cout<<"No\n"
#define yes cout<<"yes\n"
#define no cout<<"no\n"
#define yi first
#define er second
#define INF 0x3f3f3f3f
#define tulun int e[N],ne[N],h[N],w[N],idx;
#define add2(a,b) e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define add3(a,b,c) w[idx]=c,e[idx]=b,ne[idx]=h[a],h[a]=idx++;
#define T_solve() int T;cin>>T;while(T--)solve();
using namespace std;
typedef long long LL;
typedef pair<int,int> PII;
typedef pair<pair<int,int>,pair<int,int>> PIIII;
typedef double dob;
const int N=2e5+10;
const int mod=1e5+3;
struct node
{
    int a,b,w;
    int type;
    bool operator <(const node &b)
    {
        if(type!=b.type)return type<b.type;
        return w<b.w;
    }
}edge[N];
int n,m;
int p[N];
int find(int x){return p[x]==x?x:p[x]=find(p[x]);}
int ans,cnt;
void kruskal()
{
    rep2(i,1,n)p[i]=i;
    sort(edge+1,edge+m+1);
    int fj=0;
    rep2(i,1,m)
    {
        if(edge[i].type!=1)
        {
            fj=i;
            break;
        }
        int a=edge[i].a,b=edge[i].b;
        a=find(a);
        b=find(b);
        if(a!=b)cnt++;
        p[a]=b;
        ans+=edge[i].w;
    }
    rep2(i,fj,m)
    {
        int a=edge[i].a,b=edge[i].b,w=edge[i].w;
        a=find(a);
        b=find(b);
        if(cnt==n-1)break;
        if(a!=b)
        {
            p[a]=b;
            cnt++;
            ans+=w;
        }
    }
}
signed main()
{
    quick_cin();
    cin>>n>>m;
    rep2(i,1,m)
    {
        int type,a,b,w;
        cin>>type>>a>>b>>w;
        edge[i]={a,b,w,type};
    }
    kruskal();
    cout<<ans;
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dull丶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值