当前位置:网站首页>Floyd repeat
Floyd repeat
2022-07-01 09:45:00 【Wawa source】
Floyd It is based on the idea of dynamic programming , Omit the one-dimensional state , The algorithm itself has only three lines , But the questions are difficult , Need a deep understanding
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
1. shortest path
The journey of cattle
This is the question Floyd The classic application of , Attention should be paid to the classified discussion
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
using namespace std;
#define x first
#define y second
typedef pair<double, double> PDD;
const int N = 155;
const double INF = 1e18;
PDD q[N*N];
double d[N][N];
int n;
char g[N][N];
double maxd[N];
double get_dist(PDD a,PDD b)
{
double x=a.x-b.x;
double y=a.y-b.y;
return sqrt(x*x+y*y);
}
int main()
{
cin>>n;
for(int i=1;i<=n;i++)cin>>q[i].x>>q[i].y;
for(int i=1;i<=n;i++)cin>>g[i]+1 ;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(i==j)d[i][j]=0;
else if(g[i][j]=='1')d[i][j]=get_dist(q[i],q[j]);
else d[i][j]=INF;
}
}
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
d[i][j]=min(d[i][j],d[i][k]+d[k][j]);
double res1=0;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(d[i][j]<INF/2)maxd[i]=max(maxd[i],d[i][j]);
}
res1=max(res1,maxd[i]);
}
double res2=INF;
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(d[i][j]>INF/2)
res2=min(res2,maxd[i]+maxd[j]+get_dist(q[i],q[j]));
}
}
printf("%.6lf\n",max(res2,res1));
}
2. Pass closures
Sort
Similar to differential constraint , Run every time you enter an edge Floyd, Update status , See if the requirements are met
#include <iostream>
#include <cstring>
#include <algorithm>
#include<cstdio>
using namespace std;
const int N = 26;
int n,m;
bool g[N][N],d[N][N];
bool st[N];
void floyd(){
memcpy(d,g,sizeof d);
for(int k=0;k<n;k++)
for(int i=0;i<n;i++)
for(int j=0;j<n;j++)
d[i][j]|=d[i][k]&&d[k][j];
}
int check(){
for(int i=0;i<n;i++)
if(d[i][i])
return 2;
for(int i=0;i<n;i++)
for(int j=0;j<i;j++)
if(!d[i][j]&&!d[j][i])
return 0;
return 1;
}
char get_min(){
for(int i=0;i<n;i++)
if(!st[i]){
bool flag=true;
for(int j=0;j<n;j++)
if(!st[j]&&d[j][i]){
flag=false;
break;
}
if(flag){
st[i]=true;
return 'A'+i;
}
}
}
int main()
{
while(cin>>n>>m,n||m){
memset(g,0,sizeof g);
int type=0,t;
for(int i=1;i<=m;i++){
char str[5];
cin>>str;
int a=str[0]-'A',b=str[2]-'A';
if(!type){
g[a][b]=1;
floyd();
type=check();
if(type)t=i;
}
}
if(!type)puts("Sorted sequence cannot be determined.");
else if(type==2)printf("Inconsistency found after %d relations.\n", t);
else {
memset(st, 0, sizeof st);
printf("Sorted sequence determined after %d relations: ", t);
for(int i=0;i<n;i++)printf("%c",get_min());
printf(".\n");
}
}
}
3. Find the smallest ring
Sightseeing tour
This problem is more difficult
Excerpt from :Pr
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 110,INF = 0x3f3f3f3f;
int g[N][N];
int n,m;
int d[N][N];
int path[N],cnt=0;
int pos[N][N];
void get_path(int i,int j){
if(pos[i][j]==0)return ;
int k=pos[i][j];
get_path(i,k);
path[cnt++]=k;
get_path(k,j);
}
signed main()
{
cin>>n>>m;
memset(g,0x3f,sizeof g);
for(int i=1;i<=n;i++)g[i][i]=0;
for(int i=1;i<=m;i++)
{
int a,b,w;
cin>>a>>b>>w;
g[a][b]=g[b][a]=min(g[a][b],w);
}
memcpy(d,g,sizeof d);
int res=INF;
for(int k=1;k<=n;k++)
{
for(int i=1;i<k;i++)
for(int j=i+1;j<k;j++)
if((long long)d[i][j]+g[j][k]+g[k][i]<res)
{
cnt=0;
res=d[i][j]+g[j][k]+g[k][i];
path[cnt++]=i;
get_path(i,j);
path[cnt++]=j;
path[cnt++]=k;
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
if(d[i][j]>d[i][k]+d[k][j])
{
d[i][j]=d[i][k]+d[k][j];
pos[i][j]=k;
}
}
if(res==INF)puts("No solution.");
else {
for(int i=0;i<cnt;i++)cout<<path[i]<<" ";
cout<<endl;
}
}
4. Just passing by k The shortest path of the edge ( class floyd Algorithm )
Niu station
#include <iostream>
#include <cstring>
#include <algorithm>
#include <map>
using namespace std;
const int N = 110;
int n,m,k,S,E;
int g[N][N];
map<int,int>ids;
int res[N][N];
void mul(int a[][N],int b[][N],int c[][N])
{
static int tmp[N][N];
memset(tmp,0x3f,sizeof tmp);
for(int k=1;k<=n;k++)
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
tmp[i][j]=min(tmp[i][j],b[i][k]+c[k][j]);
memcpy(a,tmp,sizeof tmp);
}
void qmi()
{
memset(res,0x3f,sizeof res);
for(int i=1;i<=n;i++)res[i][i]=0;
while(k)
{
if(k&1)mul(res,res,g);
mul(g,g,g);
k>>=1;
}
}
signed main()
{
cin>>k>>m>>S>>E;
memset(g,0x3f,sizeof g);
if(!ids.count(S))ids[S]=++n;
if(!ids.count(E))ids[E]=++n;
S=ids[S],E=ids[E];
for(int i=1;i<=m;i++)
{
int a,b,c;
cin>>c>>a>>b;
if(!ids.count(a))ids[a]=++n;
if(!ids.count(b))ids[b]=++n;
a=ids[a],b=ids[b];
g[a][b]=g[b][a]=min(g[a][b],c);
}
qmi();
cout<<res[S][E]<<endl;
}
边栏推荐
- The latest masterpiece of Alibaba, which took 182 days to produce 1015 pages of distributed full stack manual, is so delicious
- 持续进阶,软通动力稳步推动云智能战略
- 全球基金和资管的股票建仓率达到15年内新低
- SQL学习笔记(02)——数据库表操作
- SQL learning notes (02) - database table operation
- Comparison between Oracle JDK and openjdk
- PHP 字符串与二进制相互转换
- 富文本实现插值
- 线程基础知识
- 数据中台咋就从“小甜甜”变成了“牛夫人”?
猜你喜欢
Scratch big fish eat small fish Electronic Society graphical programming scratch grade examination level 2 true questions and answers analysis June 2022
我喜欢两个男人。。。
LVGL V8.2字符串显示在Keil MDK上需要注意的事项(以小熊派为例)
集成积木报表报错 org.apache.catalina.core.StandardContext.filterStart 启动过滤器异常
Flinkv1.13 implementation of financial anti fraud cases
云原生到底是什么?它会是未来发展的趋势吗?
Swag init error: cannot find type definition: response Response
Spark's action operator
Initial experience of Flink, a mainstream real-time stream processing computing framework
CSDN's one-stop cloud service is open for internal testing, and new and old users are sincerely invited to grab the fresh
随机推荐
Network partition notes
JS prototype inheritance can only inherit instances, not constructors
js this丢失问题分析 及 解决方案
STM32逆变器电源设计方案,基于STM32F103控制器[通俗易懂]
直播管理项目
Computer USB, HDMI, DP various interfaces and speeds
Cortex M4 systick details
7-Zip boycotted? The callers have committed "three crimes": pseudo open source, unsafe, and the author is from Russia!
Strange, why is the ArrayList initialization capacity size 10?
PHP 字符串与二进制相互转换
CSDN一站式云服务开放内测,诚邀新老用户来抢鲜
LeetCode 344. Reverse string
js原型继承仅可继承实例而非构造器
LVGL V8.2字符串显示在Keil MDK上需要注意的事项(以小熊派为例)
Meituan P4 carefully collated microservice system architecture design manual to see the world of microservice architecture
持续进阶,软通动力稳步推动云智能战略
js重写自己的函数
Flinkv1.13实现金融反诈骗案例
SQL学习笔记(02)——数据库表操作
微信表情符号写入判决书,你发的OK、炸弹都可能成为“呈堂证供”