• P3119 [USACO15JAN]草鉴定Grass Cownoisseur(SPFA)


    题目描述

    In an effort to better manage the grazing patterns of his cows, Farmer John has installed one-way cow paths all over his farm. The farm consists of N fields, conveniently numbered 1..N, with each one-way cow path connecting a pair of fields. For example, if a path connects from field X to field Y, then cows are allowed to travel from X to Y but not from Y to X.

    Bessie the cow, as we all know, enjoys eating grass from as many fields as possible. She always starts in field 1 at the beginning of the day and visits a sequence of fields, returning to field 1 at the end of the day. She tries to maximize the number of distinct fields along her route, since she gets to eat the grass in each one (if she visits a field multiple times, she only eats the grass there once).

    As one might imagine, Bessie is not particularly happy about the one-way restriction on FJ's paths, since this will likely reduce the number of distinct fields she can possibly visit along her daily route. She wonders how much grass she will be able to eat if she breaks the rules and follows up to one path in the wrong direction. Please compute the maximum number of distinct fields she can visit along a route starting and ending at field 1, where she can follow up to one path along the route in the wrong direction. Bessie can only travel backwards at most once in her journey. In particular, she cannot even take the same path backwards twice.

    约翰有n块草场,编号1到n,这些草场由若干条单行道相连。奶牛贝西是美味牧草的鉴赏家,她想到达尽可能多的草场去品尝牧草。

    贝西总是从1号草场出发,最后回到1号草场。她想经过尽可能多的草场,贝西在通一个草场只吃一次草,所以一个草场可以经过多次。因为草场是单行道连接,这给贝西的品鉴工作带来了很大的不便,贝西想偷偷逆向行走一次,但最多只能有一次逆行。问,贝西最多能吃到多少个草场的牧草。

    输入输出格式

    输入格式:

    INPUT: (file grass.in)

    The first line of input contains N and M, giving the number of fields and the number of one-way paths (1 <= N, M <= 100,000).

    The following M lines each describe a one-way cow path. Each line contains two distinct field numbers X and Y, corresponding to a cow path from X to Y. The same cow path will never appear more than once.

    输入:

    第一行:草场数n,道路数m。

    以下m行,每行x和y表明有x到y的单向边,不会有重复的道路出现。

    输出格式:

    OUTPUT: (file grass.out)

    A single line indicating the maximum number of distinct fields Bessie

    can visit along a route starting and ending at field 1, given that she can

    follow at most one path along this route in the wrong direction.

    输出:

    一个数,逆行一次最多可以走几个草场。

    输入输出样例

    输入样例#1: 复制
    7 10 
    1 2 
    3 1 
    2 5 
    2 4 
    3 7 
    3 5 
    3 6 
    6 5 
    7 2 
    4 7 
    
    
    输出样例#1: 复制
    6 
    

    说明

    SOLUTION NOTES:

    Here is an ASCII drawing of the sample input:

    v---3-->6
    7   |  |
    ^  v  |
    |  1   |
    |   |   v
    |   v   5
    4<--2---^

    Bessie can visit pastures 1, 2, 4, 7, 2, 5, 3, 1 by traveling

    backwards on the path between 5 and 3. When she arrives at 3 she

    cannot reach 6 without following another backwards path.、








    这题缩点不难想,就先缩点,

    然后就开始想怎从dag上搞

    然后就想,路径是一个回路,枚举一条边的两个端点,假设为l , r 

    正向建立边spfa一次是原点到点i的距离,在反向建立边spfa一次,是i到原点的距离,dis l  + dis r  更新答案就行了。

    还有一个做法是分层图,每个点复制一个当作第二层,逆向走就是 r 到 l+n 建立边,那答案就是 dis 1+n

    这里的dis全是最长路







      1 #include<iostream>
      2 #include<stack>
      3 #include<vector>
      4 #include<cstring>
      5 #include<queue>
      6 
      7 using namespace std;
      8 int low[100010], pre[100010], sccno[100010], cnt[100010];
      9 int m, n, dfs_time, c;
     10 int dis[100010], dis2[100010], weight[100010];
     11 bool visited[100010];
     12 struct Node{
     13     int from;
     14     int to;
     15     int wei;
     16 };
     17 vector<Node> edges[100010];
     18 vector<Node> edges2[100010];
     19 stack<int> s;
     20 queue<int> q;
     21 
     22 void tarjan(int u){
     23     pre[u]=low[u]=++dfs_time;
     24     s.push(u);
     25     for(int i=0;i<edges[u].size();i++){
     26         int a=edges[u][i].to;
     27         if(pre[a]==0){
     28             tarjan(a);
     29             low[u]=min(low[u], low[a]);
     30         }
     31         else if(sccno[a]==0){
     32             low[u]=min(low[u], pre[a]);
     33         }
     34     }
     35     if(low[u]==pre[u]){
     36         c++;
     37         while(!s.empty()){
     38             int front=s.top(); s.pop();
     39             sccno[front]=c;
     40             if(front==u) break;
     41         }
     42     }
     43     return;
     44 }
     45 
     46 void spfa(){
     47     visited[sccno[1]]=true;
     48     dis[sccno[1]]=weight[sccno[1]];
     49     q.push(sccno[1]);
     50     while(!q.empty()){
     51         int top=q.front(); q.pop();
     52         visited[top]=false;
     53         for(int i=0;i<edges2[top].size();i++){
     54             int t=edges2[top][i].to;
     55             int w=edges2[top][i].wei;
     56             if(dis[t]<dis[top]+w){
     57                 dis[t]=dis[top]+w;
     58                 if(!visited[t]){
     59                     q.push(t);
     60                     visited[t]=true;
     61                 }
     62             }
     63         }
     64     }
     65     return;
     66 }
     67 
     68 void spfa2(){
     69     visited[sccno[1]]=true;
     70     dis2[sccno[1]]=0;
     71     q.push(sccno[1]);
     72     while(!q.empty()){
     73         int top=q.front(); q.pop();
     74         visited[top]=false;
     75         for(int i=0;i<edges[top].size();i++){
     76             int t=edges[top][i].to;
     77             int w=edges[top][i].wei;
     78 
     79             if(dis2[t]<dis2[top]+w){
     80                 dis2[t]=dis2[top]+w;
     81                 if(!visited[t]){
     82                     q.push(t);
     83                     visited[t]=true;
     84                 }
     85             }
     86         }
     87 
     88     }
     89     return;
     90 }
     91 
     92 int main(){
     93     cin >> n >> m;
     94     for(int i=0;i<m;i++){
     95         Node a;
     96         cin >> a.from >> a.to;
     97         edges[a.from].push_back(a);
     98     }
     99     for(int i=1;i<=n;i++){
    100         if(pre[i]==0) tarjan(i);
    101     }
    102 
    103     for(int i=1;i<=n;i++){
    104         weight[sccno[i]]++;
    105     }
    106     for(int i=1;i<=n;i++){
    107         for(int j=0;j<edges[i].size();j++){
    108             int t=edges[i][j].to;
    109             int f=edges[i][j].from;
    110             if(sccno[f]==sccno[t]) continue;
    111             Node a;
    112             a.from=sccno[f]; a.to=sccno[t]; a.wei=weight[sccno[t]];
    113             edges2[a.from].push_back(a);
    114         }
    115     }
    116 
    117     spfa();
    118 
    119     for(int i=1;i<=c;i++){
    120         edges[i].clear();
    121     }
    122     for(int i=1;i<=c;i++){
    123         for(int j=0;j<edges2[i].size();j++){
    124             Node a=edges2[i][j];
    125             swap(a.from, a.to);
    126             a.wei=weight[a.to];
    127             edges[a.from].push_back(a);
    128         }
    129     }
    130 
    131     memset(visited, 0, sizeof(visited));
    132     spfa2();
    133 
    134     int ans=weight[sccno[1]];
    135     for(int i=1;i<=c;i++){
    136         for(int j=0;j<edges[i].size();j++){
    137             int t=edges[i][j].to;
    138             ans=max(ans, dis[i]+dis2[t]);
    139         }
    140     }
    141     cout << ans << endl;
    142     return 0;
    143 }
  • 相关阅读:
    tftp服务器
    iw工具的使用
    六、【ioctl】应用程序和驱动程序中的ioctl
    位反转现象(Bit Flip)
    openwrt有线网卡的停用与开启
    寒假小记
    ARMLinux汇编到ADS汇编转换需要注意的问题
    c function pointer example
    (转)解决mysql“Access denied for user 'root'@'localhost'”
    c语言 面向对象的栈
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10628240.html
Copyright © 2020-2023  润新知