hdu1598 find the most comfortable road
来源:互联网 发布:mac用户名怎么改 编辑:程序博客网 时间:2024/06/04 17:45
很神奇的kruakal用法,枚举最小的边,然后kruakal找到一条可行路径,记录差值比较
看了解题报告都有点晕,自己绝对想不到这种用法。。。。。
code:
#include <cstdio>#include <algorithm>using namespace std;const int INF = 0x3fffffff;int n,m,fa[201],rank[201];struct node{ int from,to,w;}p[1001];bool cmp(const node x,const node y){ return x.w<y.w;}int find(int k){ if(k!=fa[k]) { fa[k]=find(fa[k]); } return fa[k];}void merge(int x,int y){ if(x==y) { return ; } if(rank[x]<rank[y]) { fa[x]=y; }else{ fa[y]=x; if(rank[x]==rank[y]) { rank[x]++; } }}int main(){ int i,j,a,b,x,y,ans,cas; while(~scanf("%d%d",&n,&m)) { for(i=1;i<=m;i++) { scanf("%d%d%d",&p[i].from,&p[i].to,&p[i].w); } sort(p+1,p+m+1,cmp); scanf("%d",&cas); while(cas--) { scanf("%d%d",&a,&b); ans=INF; for(i=1;i<=m;i++) { for(j=1;j<=n;j++) { fa[j]=j; rank[j]=0; } for(j=i;j<=m;j++) { x=find(p[j].from); y=find(p[j].to); merge(x,y); if(find(a)==find(b)) { x=p[j].w-p[i].w; if(x<ans) { ans=x; } break; } } if(j==m+1) { break; } } if(ans==INF) { puts("-1"); }else{ printf("%d\n",ans); } } } return 0;}
- hdu1598 find the most comfortable road
- hdu1598 find the most comfortable road
- hdu1598 find the most comfortable road
- HDU1598-find the most comfortable road
- hdu1598 find the most comfortable road
- HDU1598 find the most comfortable road
- find the most comfortable road||HDU1598
- hdu1598 find the most comfortable road 并查集
- hdu1598 find the most comfortable road 并查集+贪心
- hdu1598,find the most comfortable road,并查集,贪心
- HDU1598 find the most comfortable road 【并查集】+【枚举】
- HDU1598 find the most comfortable road【并查集】
- hdu1598 find the most comfortable road(并查集)
- HDU1598 find the most comfortable road(并查集+kruskal)
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- find the most comfortable road
- UVaOJ 458 - The Decoder
- RF coil for MRI
- 要想学好SQL语句必会的50题,也是考试常考的点
- 使用HttpModule进行权限验证
- 将一个html标记 转 json串
- hdu1598 find the most comfortable road
- JAVA的大数处理
- 32位计算机与64计算机
- XMU 1350.幸运数组 连续子数组被k整除的个数 STL
- Unity 读取xml 文件
- Unix网络编程环境搭建
- 查看VC程序内存泄露的方法
- 增强for循环用法
- JAVA读取Excel--用POI与Excel交互