当前位置:网站首页>2022.2.16
2022.2.16
2022-06-26 04:39:00 【bu_ xiang_ tutou】
10:00----10:30 13:00-----16:00 17:00---19:00
Today, I pushed the suitcase
tuixiangzi (c++)_bu_xiang_tutou The blog of -CSDN Blog
19:00---20:30
【 Templates 】 Minimum spanning tree
prim Solve the algorithm once
#include<bits/stdc++.h>
using namespace std;
#define inf 1e4+10
int dis[5009],v[5009],r[5009][5009],an,cnt,n,m;
int main()
{
int i,x,y,z;
cin>>n>>m;
for(x=1;x<=n;x++)
{
for(y=1;y<=n;y++)
{
if(x==y)
r[x][y]=0;
else
r[x][y]=inf;
}
}
for(i=1;i<=m;i++)
{
cin>>x>>y>>z;
r[x][y]=min(z,r[x][y]);
r[y][x]=min(z,r[y][x]);// Undirected graph
}
for(i=1;i<=n;i++)
dis[i]=r[1][i];
dis[1]=0;
v[1]=1;
while(cnt<n-1)
{
int min=inf,ans=-1;
for(i=1;i<=n;i++)// Find the minimum distance value
{
if(min>dis[i]&&v[i]==0)
{
min=dis[i];
ans=i;
}
}
v[ans]=1;
cnt++;
an+=dis[ans];
for(i=1;i<=n;i++)// Update distance value
{
if(v[i]==0&&dis[i]>r[ans][i])
{
dis[i]=r[ans][i];
}
}
}
int sign=0;
for(i=1;i<=n;i++)
{
if(v[i]==0)
{
sign=1;
printf("orz");
break;
}
}
if(sign==0)
cout<<an;
return 0;
} This code can only ac79, Three answers are out of time
#include<bits/stdc++.h>
using namespace std;
#define inf 1e4+10
int dis[5009],v[5009],an,n,m;
struct node{
int next;
int end;
int v;
}lu[200009];
int head[200009],cnt;
void add(int x,int y,int z)
{
cnt++;
lu[cnt].end=y;
lu[cnt].v=z;
lu[cnt].next=head[x];
head[x]=cnt;
}
int main()
{
int i,x,y,z,t=0;
cin>>n>>m;
for(i=1;i<=m;i++)
{
cin>>x>>y>>z;
add(x,y,z);
add(y,x,z);// Undirected graph
}
for(i=1;i<=n;i++)
dis[i]=inf;
dis[1]=0;
v[1]=1;
int ans=1;
while(t<n-1)
{
int min=inf;
v[ans]=1;
for(i=head[ans];i!=0;i=lu[i].next)// Find the minimum distance value
{
int b=lu[i].end,c=lu[i].v;
if(dis[b]>c&&v[b]==0)
{
dis[b]=c;
}
}
for(i=1;i<=n;i++)// Update distance value
{
if(v[i]==0&&min>dis[i])
{
min=dis[i];
ans=i;
}
}
an+=dis[ans];
t++;
}
int sign=0;
for(i=1;i<=n;i++)
{
if(v[i]==0)
{
sign++;
}
}
if(sign==1)
cout<<an;
else
printf("orz");
return 0;
}
边栏推荐
- Performance test comparison between PHP framework jsnpp and thinkphp6
- Numpy data input / output
- Minecraft 1.16.5 biochemical 8 module 1.9 version 1.18 version synchronization
- A troubleshooting of website crash due to high CPU
- Large numbers (C language)
- numpy 索引及切片
- PHP syntax summary
- ROS 笔记(07)— 客户端 Client 和服务端 Server 的实现
- 1.11 learning summary
- How to use the configured slave data source for the scheduled task configuration class scheduleconfig
猜你喜欢

企业的产品服务怎么进行口碑营销?口碑营销可以找人代做吗?

The statistics in the MySQL field become strings, and then they are converted into numbers for sorting

MySQL index details

Use of better scroll

MySQL enable logbin in Qunhui docker

Microsoft prohibits Russian users from downloading and installing win10/11

Install cenos in the virtual machine

CTF serialization and deserialization

Multipass中文文档-远程使用Multipass

TP5 distinct method paging problem
随机推荐
Be a hard worker from today on
LeetCode 94. Middle order traversal of binary tree
Condition query
A troubleshooting of website crash due to high CPU
Mysql8.0 configuring my SQL in INI file_ mode=NO_ AUTO_ CREATE_ User can start
SSH password free login, my server password free login to the other server, the other server password free login to your server
mysql高级学习(跟着尚硅谷老师周阳学习)
Tp6 controller does not exist: app\index\controller\index
How can the intelligent transformation path of manufacturing enterprises be broken due to talent shortage and high cost?
[Qunhui] Internet access + custom port
Gateway can not connect to tcp://127.0.0.1: Connection refused
Navicat connects the pit of shardingsphere sub table and sub library plug-ins
1.20 learning summary
Modify the number of Oracle connections
1.18 learning summary
Multipass中文文档-使用实例命令别名
The select option in laravel admin contains a large amount of data
[H5 development] 02 take you to develop H5 list page ~ including query, reset and submission functions
What is the best way to store chat messages in a database? [Close] - best way to store chat messages in a database? [closed]
Multipass中文文档-提高挂载性能