链接:http://poj.org/problem?id=1776

本文链接:http://www.cnblogs.com/Ash-ly/p/5458635.html

题意:

  有一个机器要完成一个作业,给你一个N*N的矩阵,如果M[i][j] = 1,说明完成第i个作业后不用重启机器,机器可以自动继续去完成第j个作业,否则M[i][j]等于0,则说明如果做完第i个作业,想要继续去做第j个作业,那么必须重启机器.对于每两个作业都有M[i][j] = 1或者M[j][i] = 1.让你求出完成这N个作业启动机器的最少次数,以及每次启动完成作业的数量和这些作业的顺序,初始时机器处于关闭状态.

解题思路:

  把一个机器看成一个点,如果第i个作业完成后不用重启机器,可以继续去做第j个机器,那么就构造有向边<Vi, Vj>.让你找到最少的序列,使得这些序列包含图中所有点一次且仅一次.由于题意的特殊性,构建出来的图是一个竞赛图,竞赛图一定存在一个哈密顿通路,包含图中所有点一次且仅一次,所以最少次数一定是1,且这一次就能完成所有的N项作业.即给出竞赛图,构造哈密顿通路.

注意:

  由于这题有毒,用scanf(),一个数字一个数字的读会超时,所以借用了某大神的输入技巧.

代码:

 #include <iostream>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#include <queue>
#include <stack>
#include <vector> using namespace std;
typedef long long LL;
const int maxN = ;
inline void read(int&a){char c;while(!(((c=getchar())>='')&&(c<='')));a=c-'';while(((c=getchar())>='')&&(c<=''))(a*=)+=c-'';} inline void Insert(int arv[], int &len, int index, int key){
if(index > len) index = len;
len++;
for(int i = len - ; i >= ; --i){
if(i != index && i)arv[i] = arv[i - ];
else{arv[i] = key; return;}
}
} void Hamilton(int ans[maxN + ], int map[maxN + ][maxN + ], int n){
int ansi = ;
ans[ansi++] = ;
for(int i = ; i <= n; i++){
if(map[i][ans[ansi - ]] == )
ans[ansi++] = i;
else{
int flag = ;
for(int j = ansi - ; j > ; --j){
if(map[i][ans[j]] == ){
flag = ;
Insert(ans, ansi, j + , i);
break;
}
}
if(!flag)Insert(ans, ansi, , i);
}
}
} int main()
{
//freopen("input.txt", "r", stdin);
int N;
while(~scanf("%d", &N)){
int map[maxN + ][maxN + ] = {};
for(int i = ; i <= N; i++){
for(int j = ; j <= N; j++){
int u;
read(u);
if(j > i && u == )
map[j][i] = ;
}
}
printf("%d\n%d\n", , N);
int ans[maxN + ] = {};
Hamilton(ans, map, N);
for(int i = ; i <= N; i++)
printf(i == ? "%d":" %d", ans[i]);
printf("\n");
}
return ;
}

POJ 1776 Task Sequences(竞赛图构造哈密顿通路)的更多相关文章

  1. poj 1776 Task Sequences

    http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...

  2. ZOJ 3332 Strange Country II (竞赛图构造哈密顿通路)

    链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=3332 本文链接:http://www.cnblogs.com/Ash-l ...

  3. 哈密顿图 哈密顿回路 哈密顿通路(Hamilton)

    本文链接:http://www.cnblogs.com/Ash-ly/p/5452580.html 概念: 哈密顿图:图G的一个回路,若它通过图的每一个节点一次,且仅一次,就是哈密顿回路.存在哈密顿回 ...

  4. UVALIVE 2954 Task Sequences

    竞赛图:图中的任意两点间有且仅有一条有向弧连接 求竞赛图中的哈密顿路的算法: 首先,由数学归纳法可证竞赛图在n>=2时必存在哈密顿路: (1)n=2时显然: (2)假设n=k时,结论成立,哈密顿 ...

  5. hdu 5424 Rikka with Graph II (BestCoder Round #53 (div.2))(哈密顿通路判断)

    http://acm.hdu.edu.cn/showproblem.php?pid=5424 哈密顿通路:联通的图,访问每个顶点的路径且只访问一次 n个点n条边 n个顶点有n - 1条边,最后一条边的 ...

  6. POJ 1239 Increasing Sequences 动态规划

    题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...

  7. POJ 3233 Matrix Power Series(构造矩阵求等比)

    Description Given a n × n matrix A and a positive integer k, find the sum S = A + A2 + A3 + … + Ak. ...

  8. POJ 3553 Task schedule

    原题链接:http://poj.org/problem?id=3553 这道题主要就是贪心思想吧,对于每个job,根据其截止时间 dj 从小到大排序,我们必须要尽快把dj最小的job完成掉,这样才能使 ...

  9. POJ 1239 Increasing Sequences(经典的两次dp)

    http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp, ...

随机推荐

  1. hdu 2874 Connections between cities (并查集+LCA)

    Connections between cities Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (J ...

  2. [Leetcode] Swap nodes in pairs 成对交换结点

    Given a linked list, swap every two adjacent nodes and return its head. For example,Given1->2-> ...

  3. URAL1696 Salary for Robots

    题目戳这里. 最长下降子序列单调队列求法. \(f_{i,j,k}\)表示考虑前\(i\)个数,\(g_1 = j,g_2 = k\)的方案数.转移: \[f_{i,j,k} = \sum_{p = ...

  4. fis难用的地方

    1. 刷新不同步,刷新的结果是前一次的修改结果2. 刷新时间非常长3. 有些代码打包不兼容,例如tween这个库,有函数yoyo:function yoyo(yoyo){}的形式,不能正确打包,会报[ ...

  5. Robot POJ - 1376

    The Robot Moving Institute is using a robot in their local store to transport different items. Of co ...

  6. centos的网络设置问题

    遭遇了多次centos的网络连接问题,现将正确配置总结下: 这里是使用vmware虚拟平台,因为涉及到中间这层,所以需要设置下: 保证centos也能连上网,首先物理机连上网,接着物理机的vmware ...

  7. bzoj 5099 [POI2018]Pionek 计算几何 极角排序

    [POI2018]Pionek Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 269  Solved: 80[Submit][Status][Disc ...

  8. 关于flume的几道题

    1,要求:监听一个tcp,udp端口41414将数据打印在控制台 # example.conf: A single-node Flume configuration # Name the compon ...

  9. Ubuntu下hadoop集群搭建

    --修改IP地址(克隆镜像后可修改可不修改) http://jingyan.baidu.com/article/e5c39bf5bbe0e739d7603396.html -------------- ...

  10. loj6043 「雅礼集训 2017 Day7」蛐蛐国的修墙方案

    传送门:https://loj.ac/problem/6043 [题解] 我们考虑这是个置换,所以一定形成了很多不相交的环. 对于每个环,我们只能选一段.不选.选一段.不选这样交替下去. 显然只有偶环 ...