poj1734Sightseeing trip
Time Limit: 1000MS | Memory Limit: 65536K | |||
Total Submissions: 6811 | Accepted: 2602 | Special Judge |
Description
In the town there are N crossing points numbered from 1 to N and M two-way roads numbered from 1 to M. Two crossing points can be connected by multiple roads, but no road connects a crossing point with itself. Each sightseeing route is a sequence of road numbers y_1, ..., y_k, k>2. The road y_i (1<=i<=k-1) connects crossing points x_i and x_{i+1}, the road y_k connects crossing points x_k and x_1. All the numbers x_1,...,x_k should be different.The length of the sightseeing route is the sum of the lengths of all roads on the sightseeing route, i.e. L(y_1)+L(y_2)+...+L(y_k) where L(y_i) is the length of the road y_i (1<=i<=k). Your program has to find such a sightseeing route, the length of which is minimal, or to specify that it is not possible,because there is no sightseeing route in the town.
Input
Output
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
Sample Output
1 3 5 2
Source
#include <cstring>
#include <cstdio>
#include <iostream>
#include <algorithm> #define inf 0x7ffffff using namespace std; int n, m,a[][],d[][],head[][],res,tot,ans[]; int main()
{
while (~scanf("%d%d", &n, &m))
{
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
{
a[i][j] = d[i][j] = inf;
head[i][j] = i;
}
for (int i = ; i <= m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
a[x][y] = a[y][x] = d[x][y] = d[y][x] = min(z, a[x][y]);
}
res = inf;
for (int k = ; k <= n; k++)
{
for (int i = ; i < k; i++)
{
for (int j = i + ; j < k; j++)
{
int temp = d[i][j] + a[i][k] + a[k][j];
if (temp < res)
{
res = temp;
tot = ;
int p = j;
while (p != i)
{
ans[++tot] = p;
p = head[i][p];
}
ans[++tot] = i;
ans[++tot] = k;
}
}
}
for (int i = ; i <= n; i++)
for (int j = ; j <= n; j++)
if (d[i][j] > d[i][k] + d[k][j])
{
d[i][j] = d[i][k] + d[k][j];
head[i][j] = head[k][j];
}
}
if (res == inf)
puts("No solution.\n");
else
{
printf("%d", ans[]);
for (int i = ; i <= tot; i++)
printf(" %d", ans[i]);
printf("\n");
}
} return ;
}
poj1734Sightseeing trip的更多相关文章
- 2018.09.15 poj1734Sightseeing trip(floyd求最小环)
跟hdu1599差不多.. 只是需要输出方案. 这个可以递归求解. 代码: #include<iostream> #include<cstdio> #include<cs ...
- poj1734Sightseeing trip——无向图求最小环
题目:http://poj.org/problem?id=1734 无向图求最小环,用floyd: 在每个k点更新f[i][j]之前,以k点作为直接连到i,j组成一个环的点,这样找一下最小环: 注意必 ...
- Lesson 4 An existing trip
Text I have just received a letter from my brother,Tim. He is in Australia. He has been there for si ...
- dp or 贪心 --- hdu : Road Trip
Road Trip Time Limit: 1000ms, Special Time Limit:2500ms, Memory Limit:65536KB Total submit users: 29 ...
- 【poj1041】 John's trip
http://poj.org/problem?id=1041 (题目链接) 题意 给出一张无向图,求字典序最小欧拉回路. Solution 这鬼畜的输入是什么心态啊mdzz,这里用vector储存边, ...
- 1301. The Trip
A number of students are members of a club that travels annually to exotic locations. Their destinat ...
- 三分 --- POJ 3301 Texas Trip
Texas Trip Problem's Link: http://poj.org/problem?id=3301 Mean: 给定n(n <= 30)个点,求出包含这些点的面积最小的正方形 ...
- 烟大 Contest1024 - 《挑战编程》第一章:入门 Problem C: The Trip(水题)
Problem C: The Trip Time Limit: 1 Sec Memory Limit: 64 MBSubmit: 19 Solved: 3[Submit][Status][Web ...
- hdu 3018 Ant Trip 欧拉回路+并查集
Ant Trip Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Problem ...
随机推荐
- 第三章 DOM的基本
节点分为不同的类型:元素节点.属性节点和文本节点 getElementById()方法 这个方法将返回一个与那个有着给定id属性值的元素节点相对应的对象.注意大小写.该方法只有一个参数.这个参数也就是 ...
- python_95_类变量的作用及析构函数
参考:http://www.cnblogs.com/alex3714/articles/5188179.html #类变量的用途:大家共有的属性,节省内存 class Person(): cn='Ch ...
- Java执行系统命令工具类(JDK自带功能)
CommandUtil.java package utils; import java.io.ByteArrayOutputStream; import java.io.IOException; im ...
- 虚IP切换原理
高可用性HA(High Availability)指的是通过尽量缩短因日常维护操作(计划)和突发的系统崩溃(非计划)所导致的停机时间,以提高系统和应用的可用性.HA系统是目前企业防止核心计算机系统因故 ...
- idea前后端分离搭建 JavaWeb项目
我们小组在开发的时候, 承诺了前后端分离, 那么就要求前端和后端需要分开搭建. 不能同时放在一个工程项目中. 大致的思路是开启两个Tomcat, 一个跑前端页面, 一个跑后端程序. 1. idea打开 ...
- Manifest文件
Manifest文件是简单的文本文件,它告知浏览器缓存的内容(或不缓存的内容) Manifest文件可以分为三个部分: 1.CAHCEMANIFEST-在此标题下列出的文件将在首次下载后进行缓存. C ...
- 《Spring源码深度解析》第三章 默认标签的解析
上一章提到了,默认标签和自定义标签要分开解析.本章重点介绍默认标签的解析.在 DefaultBeanDefinitionDocumentReader 中: private void parseDefa ...
- verilog $fopen 函数的小缺陷
system task $fopen 的argument 为1.文件名字(可以包含具体的文件路径但是注意用)2.打开方式比如"r"."w"."a&qu ...
- ProC第三弹
一.前言 我们上面已经了解Windows和Linux下的ProC开发环境,这里我们更进一步去简要介绍下ProC的预编译参数. 二.什么是预编译 预编译过程中,Pro*C/C++会自动生成C或者C++的 ...
- German Collegiate Programming Contest 2015
// Legacy Code #include <iostream> #include <cstdio> #include <cstring> #include & ...