蓝桥杯 第三十五天 最短路&二分图

目录

1.Prim算法

2.kruskal算法

3.染色法判断二分图

4.匈牙利算法

5.蓝桥杯2021年第十二届省赛真题-砝码称重 - C语言网 (dotcpp.com)

6.蓝桥杯2021年第十二届省赛真题-左孩子右兄弟 - C语言网 (dotcpp.com)


1.Prim算法

# 4 5
# 1 2 1
# 1 3 2
# 1 4 3
# 2 3 2
# 3 4 4
# ans:6
def prim():
    d=[1<<31 for i in range(n+1)]
    d[1]=0
    ans=0
    vis=[False for i in range(n+1)]
    for i in range(n):
        index=-1
        minlength=1<<31
        for j in range(1,n+1):
            if not vis[j] and d[j]v:
                d[x]=v
    return ans
n,m=map(int,input().split())
adj=[[]for i in range(n+1)]
for _ in range(m):
    x,y,z=map(int,input().split())
    adj[x].append((y,z))
    adj[y].append((x,z))
print(prim())

2.kruskal算法

# 4 5
# 1 2 1
# 1 3 2
# 1 4 3
# 2 3 2
# 3 4 4
# ans:6
def find(x):
    if x!=fa[x]:
        fa[x]=find(fa[x])
    return fa[x]
def kruskal():
    ans=0
    edge=0
    for i in range(m):
        x,y,v=adj[i]
        fax=find(x)
        fay=find(y)
        if fax!=fay:
            fa[fax]=fay
            ans+=v
            edge+=1
            if edge==n-1:
                break
    if edge==n-1:
        return ans
    else:
        return -1
n,m=map(int,input().split())
fa=[i for i in range(n+1)]
adj=[]
for _ in range(m):
    adj.append(list(map(int,input().split())))
adj.sort(key=lambda x:x[2])
print(kruskal())

3.染色法判断二分图

# 4 4
# 1 3
# 1 4
# 2 3
# 2 4
# ans:Yes
def dfs(cur,c):
    color[cur]=c
    for i in adj[cur]:
        if not color[cur]:
            if not dfs(i,3-c):
                return False
        elif color[i]==c:
            return False
    return True
n,m=map(int,input().split())
adj=[[]for i in range(n+1)]
for _ in range(m):
    x,y=map(int,input().split())
    adj[x].append(y)
color=[0 for i in range(n+1)]
flag=True
for i in range(1,n+1):
    if not color[i]:
        if not dfs(i,1):
            flag=False
            break
if flag:
    print("Yes")
else:
    print("No")

4.匈牙利算法

# 2 2 4
# 1 1
# 1 2
# 2 1
# 2 2
# ans:2
def find(cur):
    for i in adj[cur]:
        if not st[i]:
            st[i]=True
            if match[i]==0 or find(match[i]):
                match[i]=cur
                return True
    return False
n1,n2,m=map(int,input().split())
adj=[[]for i in range(n1+1)]
for _ in range(m):
    x,y=map(int,input().split())
    adj[x].append(y)
ans=0
match=[0 for i in range(n2+1)]
for i in range(1,n1+1):
    st=[False for i in range(n2+1)]
    if find(i):
        ans+=1
print(ans)

5.蓝桥杯2021年第十二届省赛真题-砝码称重 - C语言网 (dotcpp.com)

n=int(input())
a=[0]+list(map(int,input().split()))
sums=sum(a)
dp=[[0 for i in range(2*(sums+1))]for j in range(n+1)]
dp[0][0]=1
for i in range(1,n+1):
    for j in range(sums+1):
        if dp[i-1][j]==1:
            dp[i][j]=1
            dp[i][j-a[i]]=1
            dp[i][j+a[i]]=1
        if dp[i-1][j-a[i]]==1:
            dp[i][j]=1
        if dp[i-1][j+a[i]]==1:
            dp[i][j]=1
print(dp[-1][1:sums+1].count(1))

6.蓝桥杯2021年第十二届省赛真题-左孩子右兄弟 - C语言网 (dotcpp.com)

def dfs(cur):
    ans=0
    for i in adj[cur]:
        maxs=dfs(i)
        if maxs>ans:
            ans=maxs
    return ans+len(adj[cur])
n=int(input())
adj=[[]for i in range(n+1)]
for i in range(2,n+1):
    adj[int(input())].append(i)
print(dfs(1))

你可能感兴趣的:(蓝桥杯,算法,职场和发展)