zoukankan      html  css  js  c++  java
  • 限制步数的最短路

    http://acm.bnu.edu.cn/contest/problem_show.php?pid=27325

    Ping


    Time Limit: 1000 ms     Case Time Limit: 1000 ms     Memory Limit: 32768 KB
    Submit: 5     Accepted: 1
    This problem will be judged on SYSU. Original ID:1700.

    [Prev][Next]

    Description


     When the network runs into trouble, we often use the command “ping” to test whether the computer is well connected to others.
       
    For example, if we want to test whether our computer is well connected to the host of Zhongshan University, we would use the command “pingwww.zsu.edu.cn”. Then if the network works well, we would get some replies such as:
    Reply from 202.116.64.9: bytes=32 time=1ms TTL=126
    Reply from 202.116.64.9: bytes=32 time<1ms TTL=126
       
    But how can we get a reply with the whole information above, especially the time used? As follows are some details about the “Ping” process, and please note that this explanation is for this problem only and may be different from the actual “Ping” command:

    First of all, there are two kinds of message related to this “Ping” process, the echo request and echo reply.

    The echo request message contains a TimeElapsed field to record the time used since it’s sent out from the source. We can assume that in the network, each host (router, switcher, computer, and so on) is so “polite” that if the incoming message does not aim to itself, then it will update the TimeElapsed field in the message with the time used to transfer the message from the direct sender to itself, and send the message to all the other hosts that is connected to them directly. So after we send out an echo request packet, the network can “automatically” transfer it to the target host.

    Once the target host receives an echo request message, it replies with an echo reply message. The echo reply message also contains a TimeElapsed field, which is filled by the target host using the TimeElapsed field in the echo request message, and will not be changed by those intermediate computers. Then the network “automatically” transfers the reply to us again. That’s why we know whether the network is well connected or not, and the shortest time it takes to transfer a message from our computer to the target host.
      
    Actually, there is still one problem with the “Ping” process above. Suppose intermediate computers A and B are directly connected to each other, and a message aiming to C reaches A, then A will transfer the message to B since A is “polite”, and then B will send the message back to A again since B is “polite” too, then A and B are trapped into an infinite loop and keep on sending message to each other. To solve this problem, a host would throw away an incoming message if the message has been transferred for more than ten hops (Each time a host A sends a message to another host B is called one hop).

      The problem is, given the details of the network, what is the reply time we will get. Please note that this reply time will equal to the shortest time to transfer a message from the source to the target host if possible.


    Input


     The input will contain multiple test cases. In each test case, the first line is three integers n (n<=1000), m and t (0<=t<n), n is the number of hosts in the network (including our computer), m is the number of pairs of directly connected computers, and t is the target host that we would like to ping. (We always assume our computer to be host 0).
      Then there are m lines followed, each of which has three integers a (0<=a<n), b (0<=b<n) and c (0<c<=1000), indicating that host a is directly connected to b, and the time required to transfer a message from a to b or vice versa is equal to c.
      The input will be terminated by a case with n=0, which should not be processed.


    Output


    For each case, if our computer can get the reply from the target computer, print the reply time in one line. Otherwise print “no”.


    Sample Input

    3 2 2
    0 1 2
    1 2 3
    3 1 2
    0 1 2
    0 0 0

    Sample Output

    5
    no
    题意:给出一个无向连通图G,起点是0,终点是t,求不超过10步的最短路是多少;

    分析:用一般的求最短路的方法不行,可以再原来的dis基础上再开一维存步数dis[M][11];

    状态转移方程:if(dis[v][j]>dis[u][j-1]+G[u][v])  dis[v][j]=dis[u][j-1]+G[u][v];

    程序:

    #include"stdio.h"
    #include"string.h"
    #include"queue"
    #include"vector"
    #define M 1050
    #define inf 1000000000
    using namespace std;
    int dis[M][12],use[M];
    struct node
    {
        int v,w;
        node(int vv,int ww)
        {
            v=vv;
            w=ww;
        }
    };
    vector<node>edge[M];
    void spfa(int s,int n)
    {
        queue<int>q;
        int i,j;
        for(i=0;i<n;i++)
        {
            for(j=0;j<=10;j++)
                dis[i][j]=inf;
        }
        dis[s][0]=0;
        use[s]=1;
        q.push(s);
        while(!q.empty())
        {
            int u=q.front();
            q.pop();
            use[u]=0;
            for(i=0;i<(int)edge[u].size();i++)
            {
                int v=edge[u][i].v;
                for(j=1;j<=10;j++)
                {
                    if(dis[v][j]>dis[u][j-1]+edge[u][i].w)
                    {
                        dis[v][j]=dis[u][j-1]+edge[u][i].w;
                        if(!use[v])
                        {
                            q.push(v);
                            use[v]=1;
                        }
                    }
                }
            }
        }
    }
    int main()
    {
        int n,m,k,i;
        while(scanf("%d%d%d",&n,&m,&k),m||n||k)
        {
            for(i=0;i<n;i++)
                edge[i].clear();
            for(i=0;i<m;i++)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                edge[a].push_back(node(b,c));
                edge[b].push_back(node(a,c));
            }
            memset(use,0,sizeof(use));
            spfa(0,n);
            int ans=inf;
            /*for(i=0;i<n;i++)
            {
                for(int j=0;j<=10;j++)
                    printf("%d ",dis[i][j]);
                printf("
    ");
            }*/
            for(i=0;i<=10;i++)
                if(ans>dis[k][i])
                ans=dis[k][i];
            if(ans<inf)
            printf("%d
    ",ans);
            else
                printf("no
    ");
        }
        return 0;
    }
    
    *************************************************************************************
    #include"stdio.h"
    #include"string.h"
    #include"queue"
    #include"vector"
    #define M 1050
    #define inf 1000000000
    using namespace std;
    int dis[M][12],G[M][M];
    void fun(int n)
    {
        int i,j,k;
        for(i=0;i<n;i++)
            for(j=0;j<=10;j++)
            dis[i][j]=inf;
        dis[0][0]=0;
        for(k=1;k<=10;k++)
        {
            for(i=0;i<n;i++)
            {
                for(j=0;j<n;j++)
                {
                    if(G[i][j]<inf&&dis[j][k]>dis[i][k-1]+G[i][j])
                    {
                        dis[j][k]=dis[i][k-1]+G[i][j];
                    }
                }
            }
        }
    }
    int main()
    {
        int n,m,k,i,j;
        while(scanf("%d%d%d",&n,&m,&k),m||n||k)
        {
            for(i=0;i<n;i++)
                for(j=0;j<n;j++)
                G[i][j]=inf;
            for(i=0;i<m;i++)
            {
                int a,b,c;
                scanf("%d%d%d",&a,&b,&c);
                if(G[a][b]>c)
                    G[a][b]=G[b][a]=c;
            }
            fun(n);
            int ans=inf;
            /*for(i=0;i<n;i++)
            {
                for(j=0;j<=10;j++)
                    printf("%d ",dis[i][j]);
                printf("
    ");
            }*/
            for(i=0;i<=10;i++)
                if(ans>dis[k][i])
                ans=dis[k][i];
            if(ans<inf)
            printf("%d
    ",ans);
            else
                printf("no
    ");
        }
        return 0;
    }
    




  • 相关阅读:
    java异常处理 it
    java文件操作 it
    ArrayLike it
    javaProreties it
    javaset,Collections,map it
    003 Longest Substring Without Repeating Characters it
    react Video event it
    查看git地址
    Itext 生成PDF
    jar包配置文件到单独文件夹
  • 原文地址:https://www.cnblogs.com/mypsq/p/4348164.html
Copyright © 2011-2022 走看看