POJ 1734 Sightseeing trip(无向图最小环+输出路径)
#include <cstdio>
#include <string>
#include <cstring>
#include <queue>
#include <map>
#include <algorithm>
using namespace std;
#define LL __int64
#define MOD 1000000007
#define INF 0xffffff
int dp[][],g[][],pre[][];
int ans[];
int main()
{
int n,m,minz,i,j,k,sv,ev,w,num,temp;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(pre,-,sizeof(pre));
for(i = ; i <= n; i ++)
{
for(j = ; j <= n; j ++)
dp[i][j] = g[i][j] = INF;
dp[i][i] = g[i][i] = ;
}
for(i = ; i <= m; i ++)
{
scanf("%d%d%d",&sv,&ev,&w);
g[sv][ev] = dp[sv][ev] = min(dp[sv][ev],w);
g[ev][sv] = dp[ev][sv] = min(dp[ev][sv],w);
pre[sv][ev] = ev;
pre[ev][sv] = sv;
}
minz = INF;
for(i = ; i <= n; i ++)//求最小环
{
for(j = ; j <= i-; j ++)
for(k = j+; k <= i-; k ++)
{
if(minz > dp[j][k] + g[j][i] + g[i][k])
{
minz = dp[j][k] + g[j][i] + g[i][k];
num = ;
ans[] = i;
temp = j;
while(temp != -)
{
ans[num++] = temp;
temp = pre[temp][k];
}
}
}
for(j = ; j <= n; j ++)
for(k = ; k <= n; k ++)
{
if(dp[j][k] > dp[j][i] + dp[i][k])
{
dp[j][k] = dp[j][i] + dp[i][k];
pre[j][k] = pre[j][i];
}
}
}
if(minz == INF)
printf("No solution.\n");
else
{
for(i = ;i < num;i ++)
{
if(i == )
printf("%d",ans[i]);
else
printf(" %d",ans[i]);
}
}
printf("\n");
}
return ;
}
POJ 1734 Sightseeing trip(无向图最小环+输出路径)的更多相关文章
- poj 1734 Sightseeing trip判断最短长度的环
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 5590 Accepted: 2151 ...
- 【POJ1734】Sightseeing Trip 无向图最小环
题目大意:给定一个 N 个顶点的无向图,边有边权,如果存在,求出该无向图的最小环,即:边权和最小的环,并输出路径. 题解:由于无向图,且节点数较少,考虑 Floyd 算法,在最外层刚开始遍历到第 K ...
- POJ 1734 Sightseeing trip(Floyd)
题目传送门 题目中文翻译: Description 桑给巴尔岛上的阿德尔顿镇有一家旅行社,它已决定为其客户提供除了许多其他名胜之外的景点.为了尽可能地从景点赚取收入,该机构已经接受了一个精明的决定:有 ...
- POJ 1734.Sightseeing trip (Floyd 最小环)
Floyd 最小环模板题 code /* floyd最小环,记录路径,时间复杂度O(n^3) 不能处理负环 */ #include <iostream> #include <cstr ...
- POJ 1734 Sightseeing trip
题目大意: 求一个最小环. 用Floyd 求最小环算法. #include <iostream> #include <cstdlib> #include <cstdio& ...
- poj 1734 Sightseeing trip_ 最小环记录路径
题意:求最出小环,输出路径 #include <iostream> #include<cstdio> using namespace std; #define N 110 #d ...
- poj1734 Sightseeing trip【最小环】
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions:8588 Accepted:3224 ...
- URAL 1004 Sightseeing Trip(最小环)
Sightseeing Trip Time limit: 0.5 secondMemory limit: 64 MB There is a travel agency in Adelton town ...
- Codefroces Gym101572 I.Import Spaghetti-有向图跑最小环输出路径(Floyd)
暑假学的很多东西,现在都忘了,补这道题还要重新学一下floyd,有点难过,我暑假学的东西呢??? 好了,淡定,开始写题解. 这个题我是真的很难过啊,输入简直是有毒啊(内心已经画圈诅咒出题人无数次了.. ...
随机推荐
- django-jinjia 集成
现成包可以参考这里: http://niwibe.github.io/django-jinja/ Requirements Python 2.7, 3.3 or 3.4 Django 1.4, 1. ...
- KDD-CUP Proposal
From 鞠源 已有 1303 次阅读 2012-11-25 21:09 |系统分类:科研笔记|关键词:会议 领域 justify 知识 KDDCUP - Competition is a stron ...
- Maximum Gap
Given an unsorted array, find the maximum difference between the successive elements in its sorted f ...
- Reverse Pairs
For an array A, if i < j, and A [i] > A [j], called (A [i], A [j]) is a reverse pair.return to ...
- 编译 uImage
编译 uImage 和测试 u-Boot - 小猪爱拱地 - 博客频道 - CSDN.NET http://blog.csdn.net/caspiansea/article/details/38057 ...
- mysql in 查询优化
2014年11月29日21:01:01 场景:有的时候查询数据库的select in 语句中会有非常多不连续的数值,会很影响查询效率 方法:将select in 查询转换成多个select betwe ...
- Androidi性能优化之高效使用内存
应用生存期的绝大多数时间都在用于处理内存中的数据 性能主要取决于以下三个因素: a:CPU如何操作特定的数据类型 b: 数据和指令需要占用多少存储空间 c: 数据在内存中的布局 访问内存: 因为访问内 ...
- July 16th, Week 29th Saturday, 2016
A long road tests a horse's strength and a long task proves a man's heart. 路遥知马力,日久见人心. Do you have ...
- HTML5中的DOMContentLoaded 和 touchmove
Html5的出现确实解决了一部分页面交互的问题,同时它的一些特性还是没能被我们掌握,今天主要聊聊Html5中的DomcontenLoaded和touchmove事件的属性和使用: DomcontenL ...
- Python中通过cx_oracle操作ORACLE数据库的封闭函数
哈哈,看来我的SQL自动化发布,马上就全面支持ORACLE,MYSQL,POSTGRESQL,MSSQL啦... http://blog.csdn.net/swiftshow/article/deta ...