codeforces#1196F. K-th Path(最短路,思维题)
2024-09-05 07:58:17
题目链接:
https://codeforces.com/contest/1196/problem/F
题意:
在无向图的所有最短路点对中,求出第$k$大
数据范围:
$ 1 \leq k \leq 400$
分析:
其实只需要保留$k$条边,这样已经有了$k$个点对的距离小于排好序后第$k$条边的距离
再调用弗洛伊德算法$O(k^3)$
ac代码:
#include<bits/stdc++.h>
#define ll long long
#define PI acos(-1.0)
#define pa pair<int,char>
using namespace std;
const int maxn=200+10;
const int maxm=5e4+10;
const ll mod=1e9+7;
struct Edge
{
int a,b,g,s;
bool operator<(Edge & z)const
{
if(z.g==g)return s<z.s;
return g<z.g;
}
}edge[maxm];
Edge now[maxn];
ll G,S,ans=2e18;
int boss[maxn],top,n,m;
int fin(int x)
{
if(boss[x]==x)return x;
return boss[x]=fin(boss[x]);
}
void uni(int a,int b)
{
boss[fin(a)]=fin(b);
}
int main()
{
scanf("%d %d",&n,&m);
scanf("%lld %lld",&G,&S);
for(int i=1;i<=m;i++)
scanf("%d %d %d %d",&edge[i].a,&edge[i].b,&edge[i].g,&edge[i].s);
sort(edge+1,edge+1+m);
for(int i=1;i<=m;i++)
{
int cnt=0;
now[++top]=edge[i];
for(int j=top;j>=2;j--)if(now[j].s<now[j-1].s)swap(now[j],now[j-1]);
for(int j=1;j<=n;j++)boss[j]=j;
for(int j=1;j<=top;j++)
{
Edge zz=now[j];
if(fin(zz.a)==fin(zz.b))continue;
now[++cnt]=zz;
uni(zz.a,zz.b);
}
top=cnt;
if(cnt==n-1)ans=min(ans,edge[i].g*G+now[top].s*S);
//cout<<edge[i].g<<" "<<now[top].s<<endl;
}
if(ans==2e18)printf("-1\n");
else printf("%lld\n",ans);
return 0;
}
最新文章
- C#图片按比例缩放
- Greenplum第三方工具链接
- 一个人的 ClojureScript 技术栈
- Linux LVM硬盘管理之一:概念介绍
- MVC &ndash; 8.Razor 布局
- Java Hour4
- android 项目学习随笔十八(三级缓存)
- android chrome 不支持 audio/video的autoplay 属性
- WinDriver&;amp;PCIE
- 关于使用NotificationComat导致android2.3及以下版本无法显示自定义布局的解决方法.
- 转:Apache和Nginx运行原理解析
- spring security执行流程图
- JavaScript(暂时弃坑...)
- Visual Studio 2015的安装与基本使用
- iOS 中使用 XIB 自定义cell 的两种方法 以及 编译出现常见 的错误 ++++(xcode6.0之后)
- dblink实现不同用户之间的数据表访问
- win7下,使用django运行django-admin.py无法创建网站
- NLTK的安装
- mongodb2.X添加权限
- 《剑指offer》第四题(二维数组中的查找)
热门文章
- Jobs(三) HTML的form表单提交中文后,后台取出乱码的问题
- TCP协议探究(三):RTT、滑动窗口和阻塞处理
- arcgis js之调用wms服务
- css四种定位
- K2 BPM_K2受邀出席SAP研讨会:共话“智能+”时代下的赋能与转型_全业务流程管理专家
- mount -t proc none /proc
- C# TabControl 带删除
- Systemd: Service File Examples
- Linux磁盘及文件系统管理4
- PAT Basic 1085 PAT单位排行 (25 分)