poj 1776 Task Sequences

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

题意:

有一个机器要完成N个作业,

给你一个N*N的矩阵,

M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业

M[i][j]=0,表示如果做完第i个作业,想要继续去做第j个作业,那么必须重启机器

对于任意两个作业都有M[i][j] = 1或者M[j][i] = 1.

求出完成这N个作业启动机器的最少次数,以及每次启动完成作业的数量和这些作业的顺序

初始时机器处于关闭状态.

将M当做图,就是找最少的路径条数覆盖所有的点

最小路径覆盖?

但不能保证图是二分图,所以不能用匈牙利算法

题目中说对于任意两个作业都有M[i][j] = 1或者M[j][i] = 1

所以这张图是在竞赛图的基础上加了几条边

而竞赛图一定存在一条哈密顿通路

所以一定只需要一次开机完成所有作业

然后就是输出竞赛图上的一条哈密顿通路

详请参见文章http://www.cnblogs.com/TheRoadToTheGold/p/8439160.html

#include<cstdio>
#include<cstring>
#include<iostream>

using namespace std;

#define N 1001

int n;
char s[N<<1];
int e[N][N];

int front,nxt[N];

int st[N];

void Hamilton()
{
    front=1;
    memset(nxt,0,sizeof(nxt));
    for(int i=2;i<=n;++i)
    {
        if(e[front][i])
        {
            nxt[i]=front;
            front=i;
            continue;
        }
        int j,k;
        for(j=front;j;k=j,j=nxt[j])
            if(e[j][i])
            {
                nxt[i]=j;
                nxt[k]=i;
                break;
            }
        if(!j) nxt[k]=i;
    }
}

void print()
{
    printf("1\n%d\n",n);
    int now=front;
    int top=0;
    while(now)
    {
        st[++top]=now;
        now=nxt[now];
    }
    for(int i=top;i>1;--i) printf("%d ",st[i]);
    printf("%d\n",st[1]);
}

int main()
{
    while(scanf("%d",&n)!=EOF)
    {
        memset(e,false,sizeof(e));
        for(int i=1;i<=n;++i)
        {
            getchar();
            scanf("%[^\n]",s);
            int t=0;
            for(int j=0;t<n;j+=2) e[i][++t]=s[j]-‘0‘;
        }
        Hamilton();
        print();
    }
}     

Task Sequences

Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 2637   Accepted: 763   Special Judge

Description

Tom has received a lot of tasks from his boss, which are boring to deal with by hand. Fortunately,Tom got a special machine - Advanced Computing Machine (ACM) to help him. 
ACM works in a really special way. The machine can finish one task in a short time, after it‘s finishing one task, it should smoothly move to the next one, otherwise the machine will stop automatically. You must start it up again to make it continue working. Of course, the machine cannot move arbitrarily from one task to another. So each time before it starts up, one task sequence should be well scheduled. Specially, a single task also can be regarded as a sequence. In the sequence, the machine should be able to smoothly move from one task to its successor (if exists). After started up, the machine always works according to the task sequence, and stops automatically when it finishes the last one. If not all the tasks have been finished, the machine has to start up again and works according to a new sequence. Of course, the finished tasks can‘t be scheduled again. 
For some unknown reasons, it was guaranteed that for any two tasks i and j, the machine can smoothly move from i to j or from j to i or both. Because the startup process is quite slow, Tom would like to schedule the task sequences properly, so that all the tasks can be completed with minimal number of startup times. It is your task to help him achieve this goal.

Input

Input contains several testcases. For each testcase, the first line contains only one integer n, (0 < n <= 1,000), representing the number of tasks Tom has received. Then n lines follow. Each line contains n integers, 0 or 1, separated by white spaces. If the jth integer in the ith line is 1, then the machine can smoothly move from task i to task j, otherwise the machine can not smoothly move from task i to task j. The tasks are numbered from 1 to n. 
Input is terminated by end of file.

Output

For each testcase, the first line of output is only one integer k, the minimal number of startup times needed. And 2k lines follow, to describe the k task sequences. For each task sequence, the first line should contain one integer m, representing the number of tasks in the sequence. And the second line should contain m integers, representing the order of the m tasks in the sequence. Two consecutive integers in the same line should be separated by just one white space. Extra spaces are not allowed. There may be several solutions, any appropriate one is accepted.

Sample Input

3
0 1 1
1 0 1
0 0 0

Sample Output

1
3
2 1 3

Source

Asia Guangzhou 2003

原文地址:https://www.cnblogs.com/TheRoadToTheGold/p/8439853.html

时间: 2024-07-31 21:21:07

poj 1776 Task Sequences的相关文章

poj 1776 Task Sequences(哈密顿路)

题意:给定一个邻接矩阵,即一幅有向图(有环),问最少的覆盖路径为几条,求出遍历顺序: 思路:竞赛图存在哈密顿路,一定能找到一条遍历所有点的路. #include<cstdio> #include<cstring> #include<algorithm> using namespace std; int mm[1005][1005]; char ch[1005]; int nxt[50010]; int n,m; int main() { int i,j,k,head,t

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

http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp,d[i]表示分析到第i位时往前的最小长度,这样一来,d[n]就表示最后一位的最小长度. 在满足了最后一位尽量小的情况下,我们再从尾到头进行一次dp,此时d[i]表示分析到第i位时往后的最大长度.思路和第一次dp是差不多的. 1 #include<iostream> 2 #include<al

[水+dfs] poj 2034 Anti-prime Sequences

题意: 给n,m,k. 排列n~m之间的所有数. 保证相邻的2~k位之和均不为素数. 思路: 直接DFS. 代码: #include"cstdlib" #include"cstdio" #include"cstring" #include"cmath" #include"queue" #include"algorithm" #include"iostream" #in

poj 2034 Anti-prime Sequences(dfs)

//相邻的 2.3......d 之和都要不为素数 # include <algorithm> # include <stdio.h> using namespace std; int num[1010],vis[1010]; int n,m,d,cot; int flag[10010]; void init()//素数打表 { int i,j; for(i=2;i<10010;i++) { if(!flag[i]) for(j=i*i;j<10010;j=j+i) f

UVALIVE 2954 Task Sequences

竞赛图:图中的任意两点间有且仅有一条有向弧连接 求竞赛图中的哈密顿路的算法: 首先,由数学归纳法可证竞赛图在n>=2时必存在哈密顿路: (1)n=2时显然: (2)假设n=k时,结论成立,哈密顿路为V1,V2,...,Vi,...,Vk: 现添加第k+1个结点,若存在弧<Vi,Vk+1>和弧<Vk+1,Vi+1>,则可得哈密顿回路V1,V2,...,Vi,Vk+1,Vi+1,...,Vk: 若不存在上述的vi,考虑到Vk+1与v1~vk的连通状况,则只有下面种原哈密顿路的情况

poj 动态规划题目列表及总结

此文转载别人,希望自己能够做完这些题目! 1.POJ动态规划题目列表 容易:1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1208, 1276,1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740(博弈),1742, 1887, 1926(马尔科夫矩阵,求平衡), 1936, 1952, 1953, 1958, 1959, 1962, 1975,

[转] POJ DP问题

列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 1952, 1953, 1958, 1959, 1962, 1975, 1989, 2018, 2029, 2039, 2063, 20

DP题目列表/弟屁专题

声明: 1.这份列表不是我原创的,放到这里便于自己浏览和查找题目. ※最近更新:Poj斜率优化题目 1180,2018,3709 列表一:经典题目题号:容易: 1018, 1050, 1083, 1088, 1125, 1143, 1157, 1163, 1178, 1179, 1189, 1191,1208, 1276, 1322, 1414, 1456, 1458, 1609, 1644, 1664, 1690, 1699, 1740, 1742, 1887, 1926, 1936, 195

图论 500题——主要为hdu/poj/zoj

转自——http://blog.csdn.net/qwe20060514/article/details/8112550 =============================以下是最小生成树+并查集======================================[HDU]1213   How Many Tables   基础并查集★1272   小希的迷宫   基础并查集★1325&&poj1308  Is It A Tree?   基础并查集★1856   More i