zoukankan      html  css  js  c++  java
  • ZOJ-3318

    Strange Country

    Time Limit: 1 Second      Memory Limit: 32768 KB

    There are n cities in the dream country. Let's use integers from 1 to n to denote the cities. There are some roads between cities. To some strange, all the roads are bidirectional and the roads change from time to time. You have m maps of the country of different time. You are going to the dream country soon and you want to start your journey at city s and finish it at city t. Though you cannot predict the condition when you get there, you think it is useful to study the maps carefully. After studying the maps, you find that all the roads in all maps have the same length and there is an s-t path in each map. You want to choose an s-t path in each map and the paths are relatively short. Further more you don't want too many changes in the paths.

    Formally suppose you have chosen an s-t path in each map, namely P1, P2,...Pm. Let's define a path's length to be simply the number of edges in it and use LEN to denote the total length of all the paths. Let's define a function as follow: CHANGE(P1, P2,...Pm) is the number of indices i (0 < i < m) for which Pi != Pi+1. Let's define the cost function as follow: COST(P1, P2,...Pm) = LEN + CHANGE(P1, P2,...Pm). You are supposed to find the minimum cost.

    Input

    There are multiple test cases. The first line of input is an integer T (0 < T < 205) indicating the number of test cases. Then T test cases follow. The first line of each test case is 4 integers n, m, s, t (1 < n, m <= 30, 0 < s, t <= n, s != t). Then there are m map descriptions. The first line of each map description is an integer R, the number of roads in the map (0 < R <= n * (n - 1) / 2). Each of the next R lines contains two integers a, b, the two cities that road connects( 0 < a, b <= n, a != b). You can assume that for each test case there is an s-t path in each map.

    Output

    For each test case, output in a line the minimum cost defined above.

    Sample Input

    2
    3 3 2 3
    2
    1 2
    3 1
    3
    1 2
    2 3
    3 2
    2
    2 1
    2 3
    4 2 1 4
    3
    1 2
    2 3
    3 4
    3
    1 2
    2 3
    3 4
    

    Sample Output

    5
    6
    

    Hint

    Test case 1: three paths are 2-1-3, 2-3, 2-3.

    Test case 2: both paths are 1-2-3-4.


    Author: CAO, Peng
    Source: The 10th Zhejiang University Programming Contest
    Submit    Status
    #include <iostream>
    #include <stdio.h>
    #include<cmath>
    #include<algorithm>
    #include<string.h>
    #include<queue>
    #include<set>
    #define maxn 40
    using namespace std;
    int dist[maxn];
    int mark[maxn][maxn];
    int Edge[maxn][maxn];
    int mmap[maxn][maxn][maxn];
    int dp[maxn];
    int n,m,s,t,flag;
    int bfs()
    {
        memset(dist,0x3f,sizeof(dist));
        flag = dist[0];
        dist[s] = 0;
        queue<int>que;
        while(!que.empty()) que.pop();
        que.push(s);
        while(!que.empty())
        {
                        int tmp = que.front();
                        que.pop();
                        for(int i=1;i<=n;i++)
                        {
                                  if(Edge[tmp][i] == 1 && dist[i] > dist[tmp] + 1)
                                  {
                                            dist[i] = dist[tmp] + 1;
                                            que.push(i);
                                  }
                        }
        }
        return dist[t];
    }
    int main()
    {
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
    #endif // ONLINE_JUDGE
        int T;
        scanf("%d",&T);
        while(T--)
        {
            scanf("%d %d %d %d",&n,&m,&s,&t);
            int R,u,v;
            memset(mmap,0,sizeof(mmap));
            memset(mark,0,sizeof(mark));
            memset(dp,0,sizeof(dp));
            for(int i=0; i<m; i++)
            {
                scanf("%d",&R);
                for(int j=0; j<R; j++)
                {
                    scanf("%d %d",&u,&v);
                    mmap[i][u][v] = 1;
                    mmap[i][v][u] = 1;
                }
            }
            for(int i=0; i<m; i++)
            {
                memset(Edge,1,sizeof(Edge));
                for(int j=i; j<m; j++)
                {
                    for(int k=1; k<=n; k++)
                    {
                        for(int t= 1; t <=n; t++)
                        {
                                                                Edge[k][t] = Edge[k][t] & mmap[j][k][t];
                        }
                    }
                    mark[i][j] = bfs();
                }
            }
            dp[0] = mark[0][0];
            for(int i=1;i<=m;i++)
            {
                                  if(mark[0][i] != flag) dp[i] = mark[0][i] *(i+1);
                                  else dp[i] = flag;
                                  for(int j=0;j<i;j++)
                                  {
                                  if(mark[j+1][i] != flag)
                                            dp[i] = min(dp[i] ,dp[j] + mark[j+1][i]*(i-j) + 1);
                                  }
            }
            printf("%d
    ",dp[m-1]);
        }
        return 0;
    }
  • 相关阅读:
    阿里SRE体系如何支撑24小时峰值压力、220+个国家“剁手党”?
    《算法技术手册》一2.4.4 线性算法的性能
    8月7日云栖精选夜读:五分钟读懂SIGIR 2017前沿技术研究成果
    作业9 DFA最小化,语法分析初步
    作业8 非确定的自动机NFA确定化为DFA
    作业7 正规式、正规文法与自动机
    作业6 正规文法与正规式
    作业5 词法分析程序的设计与实现
    作业4 文法和语言总结与梳理
    作业3 语法树,短语,直接短语,句柄
  • 原文地址:https://www.cnblogs.com/chenyang920/p/4424530.html
Copyright © 2011-2022 走看看