【POJ 1734】 Sightseeing Trip
【题目链接】
【算法】
floyd求最小环
输出路径的方法如下,对于i到j的最短路,我们记pre[i][j]表示j的上一步
在进行松弛操作的时候更新pre即可
【代码】
#include <algorithm>
#include <bitset>
#include <cctype>
#include <cerrno>
#include <clocale>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <ctime>
#include <deque>
#include <exception>
#include <fstream>
#include <functional>
#include <limits>
#include <list>
#include <map>
#include <iomanip>
#include <ios>
#include <iosfwd>
#include <iostream>
#include <istream>
#include <ostream>
#include <queue>
#include <set>
#include <sstream>
#include <stdexcept>
#include <streambuf>
#include <string>
#include <utility>
#include <vector>
#include <cwchar>
#include <cwctype>
#include <stack>
#include <limits.h>
using namespace std;
#define MAXN 110
const int INF = 1e8; int n,m;
int g[MAXN][MAXN],mp[MAXN][MAXN],pre[MAXN][MAXN]; inline void solve()
{
int ans = INF;
vector< int > res;
for (int k = ; k <= n; k++)
{
for (int i = ; i < k; i++)
{
for (int j = i + ; j < k; j++)
{
if (g[i][j] + mp[j][k] + mp[k][i] < ans)
{
ans = g[i][j] + mp[j][k] + mp[k][i];
res.clear();
int tmp = j;
while (tmp != i)
{
res.push_back(tmp);
tmp = pre[i][tmp];
}
res.push_back(i);
res.push_back(k);
}
}
}
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
if (g[i][k] + g[k][j] < g[i][j])
{
g[i][j] = g[i][k] + g[k][j];
pre[i][j] = pre[k][j];
}
}
}
}
if (ans == INF)
{
puts("No solution.");
return;
}
for (int i = ; i < res.size() - ; i++) printf("%d ",res[i]);
printf("%d\n",res[res.size()-]);
} int main()
{ scanf("%d%d",&n,&m);
for (int i = ; i <= n; i++)
{
for (int j = ; j <= n; j++)
{
g[i][j] = mp[i][j] = INF;
pre[i][j] = i;
}
}
for (int i = ; i <= m; i++)
{
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
if (w < g[u][v])
g[u][v] = mp[u][v] = g[v][u] = mp[v][u] = w;
}
solve(); return ; }
【POJ 1734】 Sightseeing Trip的更多相关文章
- POJ 1734:Sightseeing trip
Sightseeing trip Time Limit: 1000MS Memory Limit: 65536K Total Submissions: Accepted: Special Judge ...
- 【POJ 2486】 Apple Tree(树型dp)
[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 8981 Acce ...
- bzoj 2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MB Description ftiasch是个十分受女生欢迎的同学,所以 ...
- 【链表】BZOJ 2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 382 Solved: 111[Submit][S ...
- BZOJ2288: 【POJ Challenge】生日礼物
2288: [POJ Challenge]生日礼物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 284 Solved: 82[Submit][St ...
- BZOJ2293: 【POJ Challenge】吉他英雄
2293: [POJ Challenge]吉他英雄 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 80 Solved: 59[Submit][Stat ...
- BZOJ2287: 【POJ Challenge】消失之物
2287: [POJ Challenge]消失之物 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 254 Solved: 140[Submit][S ...
- BZOJ2295: 【POJ Challenge】我爱你啊
2295: [POJ Challenge]我爱你啊 Time Limit: 1 Sec Memory Limit: 128 MBSubmit: 126 Solved: 90[Submit][Sta ...
- BZOJ2296: 【POJ Challenge】随机种子
2296: [POJ Challenge]随机种子 Time Limit: 1 Sec Memory Limit: 128 MBSec Special JudgeSubmit: 114 Solv ...
随机推荐
- 搭建双塔(vijos 1037)
描述 2001年9月11日,一场突发的灾难将纽约世界贸易中心大厦夷为平地,Mr. F曾亲眼目睹了这次灾难.为了纪念“9?11”事件,Mr. F决定自己用水晶来搭建一座双塔. Mr. F有N块水晶,每块 ...
- Tsinghua OJ Zuma
Description Let's play the game Zuma! There are a sequence of beads on a track at the right beginnin ...
- Codeforces917C. Pollywog
$n \leq 1e8$个石头,$x \leq 8$个蝌蚪一开始在最左边$x$个石子,要跳到最右的$x$个,每次只能最左边的蝌蚪跳一次,一个石头不能站两个蝌蚪,跳可以跳$1到k,x \leq k \l ...
- php 之mysql安全
php 之mysql安全 原文:https://www.cnblogs.com/mafeng/p/5939329.html. 请浏览原文. 一.服务器配置方面. (1) 打开php的安全模式 php的 ...
- IText 生成pdf,处理table cell列跨页缺失的问题
/** * 创建(table)PDF,处理cell 跨页处理 * @param savePath(需要保存的pdf路径) * @param pmbs (数据库查询的数据) ...
- 实现浏览器兼容的innerText
今天学习到了FF不支持innerText,而IE.chrome.Safari.opera均支持innerText. 为了各个浏览器能兼容innerText,必须对js做一次封装. 为啥能实现兼容呢?原 ...
- Nexus搭建Maven私有仓库
原文:http://blog.csdn.net/rickyit/article/details/54927101 前言 Nexus Repository Manager is a Javaapplic ...
- Spring4.0MVC学习资料,注解自己主动扫描bean,自己主动注入bean(二)
Spring4.0的新特性我们在上一章已经介绍过了. 包含它对jdk8的支持,Groovy Bean Definition DSL的支持.核心容器功能的改进,Web开发改进.測试框架改进等等.这张我们 ...
- Office WORD如何取消开始工作右侧栏
工具-选项-视图,取消勾选"启动任务窗格"
- 基于 Vue.js 之 iView UI 框架非工程化实践记要 使用 Newtonsoft.Json 操作 JSON 字符串 基于.net core实现项目自动编译、并生成nuget包 webpack + vue 在dev和production模式下的小小区别 这样入门asp.net core 之 静态文件 这样入门asp.net core,如何
基于 Vue.js 之 iView UI 框架非工程化实践记要 像我们平日里做惯了 Java 或者 .NET 这种后端程序员,对于前端的认识还常常停留在 jQuery 时代,包括其插件在需要时就引 ...