zoukankan      html  css  js  c++  java
  • 旅行商(TSP)

    清华OJ——数据结构与算法实验(中国石油大学)

    旅行商(TSP)


    Description

    Shrek is a postman working in the mountain, whose routine work is sending mail to n villages. Unfortunately, road between villages is out of repair for long time, such that some road is one-way road. There are even some villages that can’t be reached from any other village. In such a case, we only hope as many villages can receive mails as possible.

    Shrek hopes to choose a village A as starting point (He will be air-dropped to this location), then pass by as many villages as possible. Finally, Shrek will arrived at village B. In the travelling process, each villages is only passed by once. You should help Shrek to design the travel route.

    Input

    There are 2 integers, n and m, in first line. Stand for number of village and number of road respectively.

    In the following m line, m road is given by identity of villages on two terminals. From v1 to v2. The identity of village is in range [1, n].

    Output

    Output maximum number of villages Shrek can pass by.

    Example

    Input

    4 3
    1 4
    2 4
    4 3
    

    Output

    3
    

    Restrictions

    1 <= n <= 1,000,000

    0 <= m <= 1,000,000

    These is no loop road in the input.

    Time: 2 sec

    Memory: 256 MB

    Hints

    Topological sorting

    描述

    Shrek是一个大山里的邮递员,每天负责给所在地区的n个村庄派发信件。但杯具的是,由于道路狭窄,年久失修,村庄间的道路都只能单向通过,甚至有些村庄无法从任意一个村庄到达。这样我们只能希望尽可能多的村庄可以收到投递的信件。

    Shrek希望知道如何选定一个村庄A作为起点(我们将他空投到该村庄),依次经过尽可能多的村庄,路途中的每个村庄都经过仅一次,最终到达终点村庄B,完成整个送信过程。这个任务交给你来完成。

    输入

    第一行包括两个整数n,m,分别表示村庄的个数以及可以通行的道路的数目。

    以下共m行,每行用两个整数v1和v2表示一条道路,两个整数分别为道路连接的村庄号,道路的方向为从v1至v2,n个村庄编号为[1, n]。

    输出

    输出一个数字,表示符合条件的最长道路经过的村庄数。

    样例

    见英文题面

    限制

    1 ≤ n ≤ 1,000,000

    0 ≤ m ≤ 1,000,000

    输入保证道路之间没有形成环

    时间:2 sec

    空间:256 MB

    提示

    拓扑排序

     1 #include<cstdio>
     2 #include<iostream>
     3 #include<cstring>
     4 #define N 1100000
     5 using namespace std;
     6 
     7 struct ss
     8 {
     9     int v,next;
    10 };
    11 ss edg[2*N];
    12 int head[N*2],sum_edge=0;
    13 
    14 void addedge(int u,int v)
    15 {
    16     edg[sum_edge]=(ss){v,head[u]};
    17     head[u]=sum_edge++;
    18 }
    19 
    20 int du[N];
    21 int Stack[N],top=0;
    22 int arr[N],c1=0;
    23 int dp[N]={0};
    24 
    25 int read()
    26 {
    27     int now=0;
    28     char ch=getchar();
    29     while(!(ch>='0'&&ch<='9'))ch=getchar();
    30     while(ch>='0'&&ch<='9')
    31     {
    32         now=now*10+ch-'0';
    33         ch=getchar();
    34     }
    35     return now;
    36 }
    37 
    38 int main()
    39 {
    40     memset(head,-1,sizeof(head));
    41     int n,m,u,v;
    42     n=read();
    43     m=read();
    44     
    45     while(m--)
    46     {
    47         u=read();
    48         v=read();
    49         addedge(u,v);
    50         du[v]++;
    51     } 
    52     
    53     for(int i=1;i<=n;i++)if(!du[i])Stack[top++]=i;
    54     
    55     while(top)
    56     {
    57         int now=Stack[--top];
    58         arr[c1++]=now;
    59         
    60         for(int i=head[now];i!=-1;i=edg[i].next)
    61         {
    62             v=edg[i].v;
    63             du[v]--;
    64             if(!du[v])Stack[top++]=v;
    65         }
    66     }
    67     
    68     int ans=0;
    69     
    70     for(int i=0;i<c1;i++)
    71     {
    72         u=arr[i];
    73         dp[u]++;
    74         ans=max(ans,dp[u]);
    75         for(int i=head[u];i!=-1;i=edg[i].next)
    76         {
    77              v=edg[i].v;
    78              dp[v]=max(dp[v],dp[u]);
    79         }
    80     }
    81     
    82     printf("%d\n",ans);
    83     return 0;
    84 } 
  • 相关阅读:
    mongodb浅析
    java8学习的一点总结
    mysql 存储引擎对索引的支持
    Ubuntu16.04下安装OpenCV2.4.13
    windows配置远程桌面连接到ubuntu
    Ubuntu16.04调整屏幕分辨率至1920*1080
    Ubuntu 16.04 安装NodeJs
    Ubuntu安装SSH服务
    基于MNIST数据集使用TensorFlow训练一个包含一个隐含层的全连接神经网络
    基于MNIST数据集使用TensorFlow训练一个没有隐含层的浅层神经网络
  • 原文地址:https://www.cnblogs.com/sylvia1111/p/15613033.html
Copyright © 2011-2022 走看看