网络探测

Problem 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 “ping www.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
#include 
#include 
#include 
#include 
using namespace std;
const int inf = 100000000;
struct node
{
    int v, w;
    node(int x,int y)
    {
        v = x;
        w = y;
    }
};
vector vec[1010];
int n, m, t, vis[1010], dis[1010][11];
void spfa(int s)
{
    queue q;
    while(!q.empty()) q.pop();
    q.push(s);
    for(int i = 0; i < n; ++ i)
        for(int j = 0; j <= 10; ++ j)
        dis[i][j] = inf;
    dis[s][0] = 0;
    memset(vis,0,sizeof(vis));
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u]=0;
        for(int i = 0;i < vec[u].size();i++)
        {
            int v = vec[u][i].v;
            for(int j = 1; j <= 10; j ++)
            {
                if(dis[v][j] > dis[u][j-1] + vec[u][i].w)
                {
                    dis[v][j] = dis[u][j-1] + vec[u][i].w;
                    if(!vis[v])
                    {
                        q.push(v);
                        vis[v]=1;
                    }
                }
            }
        }
    }
}
int  main()
{
//    freopen("a.txt","r",stdin);
    int  i;
    while(cin >> n >> m >> t,n+m+t)
    {
        for(i = 0; i <= n; ++ i) vec[i].clear();
        int a, b, w;
        for(i = 1; i <= m; ++ i)
        {
            cin >> a >> b >> w;
            vec[a].push_back(node(b,w));
            vec[b].push_back(node(a,w));
        }
        spfa(0);
        int ans = inf;
        for(i = 0; i <= 10; ++ i)
            if(ans > dis[t][i])
            ans = dis[t][i];
        if(ans < inf) cout << ans << endl;
        else cout << "no" << endl;
    }
    return 0;
}

 
 

你可能感兴趣的:(图论,DP)