第二周acm jnu

学习内容:

1.分层图最短路

给每两层之间的任意两点连一条边权为C的边,然后跑一遍最短路

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<functional>
#include<stdlib.h>
#include<ctype.h>
#include<sstream>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;
const int maxn = 100010, M = 20000010, inf = 0x3f3f3f3f;
#define x first
#define y second
typedef pair<int,int> PII;
int h[maxn], e[M], w[M], ne[M], idx;
bool st[maxn];
vector<int> v[maxn];
int dist[maxn];
int n, m, k;
void add(int a, int b, int c)
{
    e[idx] = b, w[idx] = c, ne[idx] = h[a], h[a] = idx ++;
}
void Dijkstra()
{
    memset(dist, 0x3f, sizeof dist);
    dist[1] = 0;
    priority_queue<PII, vector<PII>, greater<PII>> heap;
    heap.push({0, 1});
    while(heap.size())
    {
        auto t = heap.top();
        heap.pop();
        int u = t.y;
        if(st[u]) continue;
        st[u] = true;
        for(int i = h[u]; i != -1; i = ne[i]){
            int j = e[i];
            if(dist[j] > dist[u] + w[i]){
                dist[j] = dist[u] + w[i];
                heap.push({dist[j], j});
            }
        }
    }
}
int main()
{
    memset(h, -1, sizeof h);
    scanf("%d %d %d", &n, &m, &k);
    int max_depth = 0;
    for(int i = 1; i <= n; i ++){
        int x; scanf("%d", &x);
        v[x].push_back(i);
        max_depth = max(max_depth, x);
    }
    for(int i = 0; i < m; i ++){
        int a, b, c; scanf("%d %d %d", &a, &b, &c);
        add(a, b, c); add(b, a, c);
    }
    for(int i = 1; i < max_depth; i ++){
       vector<int> t1 = v[i], t2 = v[i + 1];
        for(int j = 0; j < t1.size(); j ++){
            for(int p = 0; p < t2.size(); p ++){
                add(t1[j], t2[p], k);
                add(t2[p], t1[j], k);
            }
        }
    }
    Dijkstra();
    if(dist[n] >= inf / 2) puts("-1");
    else printf("%d\n", dist[n]);
}

 


2.最小生成树模版 练习记忆一下


3.树(数论)

将树分割为不超过 k 个连通块,每个连通块颜色不同。若将树分割为  i个连通块,则需要删去i-1  条边,故方案数为 c i-1 n-1 。同时,要从  k种颜色中选出 i 中颜色染色,而且是有顺序的,故方案数为  a i k。

因为n>k.颜色可1~k枚举

#include<iostream>
#include<algorithm>
#include<string>
#include<string.h>
#include<set>
#include<functional>
#include<stdlib.h>
#include<ctype.h>
#include<sstream>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<vector>
#include<cstdio>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
typedef long long ll;
using namespace std;

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*10+ch-48;ch=getchar();}
    return x*f;
}
const ll mod=1e9+7;
const ll maxn=310;
ll n,k,ans,inv[maxn],f[maxn];
ll C(ll x,ll y){
    return f[x]*inv[y]%mod*inv[x-y]%mod;
}
ll A(ll x,ll y){
    return f[x]*inv[x-y]%mod;
}
int main(){
    ios::sync_with_stdio(false);
    cin>>n>>k;
    inv[0]=f[0]=inv[1]=f[1]=1;
    for(ll i=2;i<maxn;i++)
        inv[i]=((mod-mod/i)*inv[mod%i])%mod,f[i]=i;
    for(ll i=2;i<maxn;i++)
        inv[i]=(inv[i]*inv[i-1])%mod,f[i]=(f[i]*f[i-1])%mod;
    for(ll i=1;i<=k&&i<=n;i++)
        ans=(ans+(C(n-1,i-1)*A(k,i)%mod))%mod;
    cout<<ans;
    return 0;
}

这周状态不好题写的少,而且图论学太少感觉遇到瓶颈,头铁写了几道图论难题都gg了,看来图论要加强啊

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值