题目:Sightseeing trip

题意:求一个图中最小环,输出路径。

#include <iostream>
#include <string.h>
#include <stdio.h>
using namespace std;
const int N=105;
const int INF=9999999;
int map[N][N],dist[N][N];
int road[N][N],path[N];
int m,n,cnt,ans;
void Init()
{
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
dist[i][j]=INF;
road[i][j]=0;
}
}
}
/**记录最小环的路径*/
void Record(int s,int t)
{
if(road[s][t])
{
Record(s,road[s][t]);
Record(road[s][t],t);
}
else path[cnt++]=t;
}
void Floyd()
{
int i,j,k;
ans=INF;
for(k=1;k<=n;k++)
{
/**最小负环的判定*/
for(i=1;i<k;i++)
{
for(j=i+1;j<k;j++)
{
if(ans>dist[i][j]+map[i][k]+map[k][j])
{
ans=dist[i][j]+map[i][k]+map[k][j];
cnt=0;
path[cnt++]=i;
Record(i,j);
path[cnt++]=k;
}
}
}
/**正常floyd部分*/
for(i=1;i<=n;i++)
{
for(j=1;j<=n;j++)
{
if(dist[i][j]>dist[i][k]+dist[k][j])
{
dist[i][j]=dist[i][k]+dist[k][j];
road[i][j]=k;
}
}
}
}
}
int main()
{
int i,j,u,v,w;
while(cin>>n>>m)
{
Init();
while(m--)
{
cin>>u>>v>>w;
if(w<dist[u][v]) /**如果有重边,就取最小的权值*/
{
dist[u][v]=w;
dist[v][u]=w;
}
}
for(int i=1;i<=n;i++)
for(int j=1;j<=n;j++)
map[i][j]=dist[i][j];
Floyd();
if(ans==INF) puts("No solution.");
else
{
cout<<path[0];
for(int i=1;i<cnt;i++)
cout<<" "<<path[i];
cout<<endl;
}
}
return 0;
}

POJ1734(floyd求最小环的路径)相关推荐

  1. 多源最短路径Floyd、Floyd求最小环【模板】

    Floyd算法:用来找出每对点之间的最短距离.图可以是无向图,也可以是有向图,边权可为正,也可以为负,唯一要求是不能有负环.  1.初始化:将Map[][]中的数据复制到Dist[][]中作为每对顶点 ...

  2. 2017百度之星程序设计大赛 - 资格赛【1001 Floyd求最小环 1002 歪解(并查集),1003 完全背包 1004 01背包 1005 打表找规律+卡特兰数】...

    度度熊保护村庄 Accepts: 13 Submissions: 488 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/3276 ...

  3. floyd求最小环 模板

    http://www.cnblogs.com/Yz81128/archive/2012/08/15/2640940.html 求最小环 floyd求最小环 2011-08-14 9:42 1 定义: ...

  4. 最小环 floyd java_干货|Floyd求最小环(CF Shortest Cycle)

    作者:Water_Fox 来源:牛客网 You are given nn integer numbers a1,a2,-,ana1,a2,-,an. Consider graph on nn node ...

  5. floyd求最小环——poj1734Sightseeing trip

    题目链接 Description There is a travel agency in Adelton town on Zanzibar island. It has decided to offe ...

  6. hdu 1599(Floyd求最小环)

    find the mincost route Time Limit: 1000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/O ...

  7. POJ1734 Sightseeing trip floyd求最小环问题

    问题描述 m个点, m条边, 求图中存在的路径最小的环 样例 Sample Input 5 7 1 4 1 1 3 300 3 1 10 1 2 16 2 3 100 2 5 15 5 3 20 Sa ...

  8. hdu 1599 find the mincost route(找无向图最小环)(floyd求最小环)

    ps(我到今天才知道Floyd的核心思想是动态规划==) hdu 1599 find the mincost route(找无向图最小环) 注意!这里写成   #define data 0x3f3f3 ...

  9. Acwing 344.观光之旅(Floyd求最小环)

    Acwing 344.观光之旅 题意 给定一张无向图,求图中一个至少包含 3 个点的环,环上的节点不重复,并且环上的边的长度之和最小. 该问题称为无向图的最小环问题. 你需要输出最小环的方案,若最小环 ...

最新文章

  1. mysql总结 博客园_mysql总结
  2. 如何用Linux写c程序并编译运行
  3. [C#.NET 拾遗补漏]16:几个常见的TAP异步操作
  4. Jmeter 场景设计
  5. python模型保存save_浅谈keras保存模型中的save()和save_weights()区别
  6. 【java】初始化一个指定大小的list,在指定位置set存入元素,下标越界
  7. Mosquito的优化——订阅树优化(八)
  8. python基础知识——异常
  9. mybatis --XML 映射配置文件
  10. 计算机体系结构----指令流水线吞吐率、效率计算
  11. 好工具推荐系列:Github客户端GitHub Desktop使用方法
  12. bt种子制作php,BT种子制作
  13. 自助建站平台实力比拼:凡科、微企点、建站之星、宝华建站、微魔方、上线了...
  14. 微信小程序布局技巧(一)
  15. CSDN第二篇文章· 爬虫突破封禁的6种常见方法
  16. 华为网络设备介绍及基础配置命令
  17. mysql怎么创建blog_「MySQL创建与删除数据库」- 海风纷飞Blog
  18. 认知LTE簇优化和全网优化
  19. 【NLP】文本匹配——Enhanced LSTM for Natural Language Inference阅读与总结
  20. ppt如何删除所有特效?

热门文章

  1. AnnotationConfigUtils 处理注解Bean 定义类中的通用注解
  2. 负责域名解析的DNS服务
  3. aop简介-基于cglib的动态
  4. 格式化输出字符串变量
  5. php5.6.16,OSX 10.11 中重新编译PHP5.6.16问题
  6. 树莓派3 mysql端口_树莓派3 之 安装Mysql服务
  7. python 消息队列、异步分布式
  8. Keras Data augmentation(数据扩充)
  9. Xamarin.Form 初学 之 服务引用-WCF服务引用
  10. WinStore开发知识导航集锦