zoukankan      html  css  js  c++  java
  • bzoj [POI2007]旅游景点atr 状态压缩+Dij

     [POI2007]旅游景点atr

    Time Limit: 30 Sec  Memory Limit: 357 MB
    Submit: 2258  Solved: 595
    [Submit][Status][Discuss]

    Description

      FGD想从成都去上海旅游。在旅途中他希望经过一些城市并在那里欣赏风景,品尝风味小吃或者做其他的有趣
    的事情。经过这些城市的顺序不是完全随意的,比如说FGD不希望在刚吃过一顿大餐之后立刻去下一个城市登山,
    而是希望去另外什么地方喝下午茶。幸运的是,FGD的旅程不是既定的,他可以在某些旅行方案之间进行选择。由于
    FGD非常讨厌乘车的颠簸,他希望在满足他的要求的情况下,旅行的距离尽量短,这样他就有足够的精力来欣赏风
    景或者是泡MM了^_^.整个城市交通网络包含N个城市以及城市与城市之间的双向道路M条。城市自1至N依次编号,道
    路亦然。没有从某个城市直接到它自己的道路,两个城市之间最多只有一条道路直接相连,但可以有多条连接两个
    城市的路径。任意两条道路如果相遇,则相遇点也必然是这N个城市之一,在中途,由于修建了立交桥和下穿隧道
    ,道路是不会相交的。每条道路都有一个固定长度。在中途,FGD想要经过K(K<=N-2)个城市。成都编号为1,上海
    编号为N,而FGD想要经过的N个城市编号依次为2,3,…,K+1.举例来说,假设交通网络如下图。FGD想要经过城市2,3,
    4,5,并且在2停留的时候在3之前,而在4,5停留的时候在3之后。那么最短的旅行方案是1-2-4-3-4-5-8,总长度为1
    9。注意FGD为了从城市2到城市4可以路过城市3,但不在城市3停留。这样就不违反FGD的要求了。并且由于FGD想要
    走最短的路径,因此这个方案正是FGD需要的。

    Input

      第一行包含3个整数N(2<=N<=20000),M(1<=M<=200000),K(0<=K<=20),意义如上所述。

    Output

      只包含一行,包含一个整数,表示最短的旅行距离。

    Sample Input

    8 15 4
    1 2 3
    1 3 4
    1 4 4
    1 6 2
    1 7 3
    2 3 6
    2 4 2
    2 5 2
    3 4 3
    3 6 3
    3 8 6
    4 5 2
    4 8 6
    5 7 4
    5 8 6
    3
    2 3
    3 4
    3 5

    Sample Output

    19

    HINT

     上面对应于题目中给出的例子。

    Source

    题解:k十分的小然后处理出两两之间的最短路,然后状态压缩dp一下

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstdlib>
     4 #include<algorithm>
     5 #include<cmath>
     6 #include<cstring>
     7 #include<set>
     8 #include<queue>
     9 #define pa pair<int,int>
    10 #define inf 1000000000
    11 #define ll long long
    12 using namespace std;
    13 int read()
    14 {
    15     int x=0,f=1;char ch=getchar();
    16     while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
    17     while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    18     return x*f;
    19 }
    20 int n,m,K,cnt,ed;
    21 int bin[22],a[22];
    22 int dis[22][22],d[20005],last[20005];
    23 int f[1048576][22];
    24 bool vis[20005];
    25 struct data{int to,next,v;}e[400005];
    26 void insert(int u,int v,int w)
    27 {
    28     e[++cnt].to=v;e[cnt].next=last[u];last[u]=cnt;e[cnt].v=w;
    29     e[++cnt].to=u;e[cnt].next=last[v];last[v]=cnt;e[cnt].v=w;
    30 }
    31 void dijkstra(int x)
    32 {
    33     priority_queue<pa,vector<pa>,greater<pa> >q;
    34     for(int i=1;i<=n;i++)d[i]=inf;
    35     for(int i=1;i<=n;i++)vis[i]=0;
    36     d[x]=0;q.push(make_pair(0,x));
    37     while(!q.empty())
    38     {
    39         int now=q.top().second;q.pop();
    40         if(vis[now])continue;vis[now]=1;
    41         for(int i=last[now];i;i=e[i].next)
    42             if(d[now]+e[i].v<d[e[i].to])
    43             {
    44                 d[e[i].to]=d[now]+e[i].v;
    45                 q.push(make_pair(d[e[i].to],e[i].to));
    46             }
    47     }
    48     for(int i=1;i<=K+1;i++)
    49         dis[x][i]=d[i];
    50     dis[x][0]=d[n];
    51 }
    52 void dp()
    53 {
    54     for(int now=0;now<=ed;now++)
    55         for(int x=1;x<=K+1;x++)
    56             if(f[now][x]!=-1)
    57                 for(int i=2;i<=K+1;i++)
    58                 {
    59                     int to=(now|bin[i-2]);
    60                     if((now&a[i])==a[i])
    61                         if(f[to][i]>f[now][x]+dis[x][i]||f[to][i]==-1)
    62                             f[to][i]=f[now][x]+dis[x][i];
    63                 }
    64 }
    65 int main()
    66 {
    67     bin[0]=1;for(int i=1;i<22;i++)bin[i]=bin[i-1]<<1;
    68     n=read();m=read();K=read();ed=bin[K]-1;
    69     for(int i=1;i<=m;i++)
    70     {
    71         int u=read(),v=read(),w=read();
    72         insert(u,v,w);
    73     }
    74     for(int i=1;i<=K+1;i++)dijkstra(i);
    75     int x=read();
    76     for(int i=1;i<=x;i++)
    77     {
    78         int u=read(),v=read();
    79         a[v]+=bin[u-2];
    80     }
    81     memset(f,-1,sizeof(f));
    82     f[0][1]=0;
    83     dp();
    84     int ans=inf;
    85     for(int i=1;i<=K+1;i++)
    86         if(f[ed][i]!=-1)ans=min(ans,f[ed][i]+dis[i][0]);
    87     printf("%d",ans);
    88     return 0;
    89 }
  • 相关阅读:
    my first android test
    VVVVVVVVVV
    my first android test
    my first android test
    my first android test
    ini文件
    ZZZZ
    Standard Exception Classes in Python 1.5
    Python Module of the Week Python Module of the Week
    my first android test
  • 原文地址:https://www.cnblogs.com/fengzhiyuan/p/8834313.html
Copyright © 2011-2022 走看看