zoukankan      html  css  js  c++  java
  • Routing

    As part of his new job at the IT security department of his company, Bob got the task to track how fast messages between the companies’ different offices are transmitted through the internet.
    Since some offices are currently rebuilt and not finished yet he is not able to simply measure the transmission speed but needs to compute it somehow. Therefore, Bob created a map of all servers that may be involved in routing the packages through the internet. He also gathered the
    times each server needs to process a message. The total processing time of a message is the sum of the processing times of the sender, all servers along the path, and the receiver. Furthermore, Bob read that messages are sent through the network of servers along a path such that the total
    processing time of all servers on the path is minimal.
    Bob thought that it might be an easy problem to compute the total transmission time between two offices, but he forgot the intelligence agencies! Each server on the internet is controlled by some agency that can decide which packages are routed and which of them are not. All servers are configured in a way that they read all incoming data, since gathering all kind of information is exactly what the intelligence agencies want to do, but not all data is forwarded to other servers. 
    Each server has a list of pairs of other servers such that messages from the first of them are not sent to the second one. Can you still help Bob to compute how fast his messages will be transmitted?

    输入

    The input consists of:
    • one line with an integer n (2 ≤ n ≤ 100), where n is the number of servers labeled from 1 to n;
    • n blocks describing the servers. Each server is described by:
    – one line with two integers m (0 ≤ m ≤ n − 1) and t (0 ≤ t ≤ 1000), where m is the number of outgoing connections from this server and t is the processing time of this server;
    – m lines with two integers s (0 ≤ s ≤ n − 1) and x (1 ≤ x ≤ n) and s more distinct integers a 1 , . . . , a s (1 ≤ a j ≤ n, aj ≠ i for all 1 ≤ j ≤ s) indicating that server i sends messages to server x, but only if it was not directly transmitted from one of the servers a 1 , . . . , a s to server i.
    Bob’s messages start at server 1 and should go to server n.

    输出

    Output the sum of the processing times of all servers on the shortest path for Bob’s message including the first and last one, or “impossible” if there is no such path.

    样例输入

    4
    2 10
    0 2
    0 3
    1 1
    1 4 1
    2 10
    1 2 1
    0 4
    0 10
    

    样例输出

    30
    

    提示

    最短路,dist[i][j]表示当前点是i,其前驱是j的最短路。
    每次取出优先队列队头,枚举前驱,看这个前驱是否对某条那边合法。
    暴力转移即可。
    #include <bits/stdc++.h>
    using namespace std;
    #define maxn 105
    int a[maxn];
    int n;
    struct Edge
    {
        int v,next;
    };
    map<pair<int,int>,int> mp;
    struct Map
    {
        int head[maxn];
        int cnt;
        Edge edge[100000];
        void init()
        {
            memset(head,-1, sizeof(head));
            cnt=0;
        }
        void addedge(int u,int v)
        {
            edge[cnt].v=v;
            edge[cnt].next=head[u];
            mp[make_pair(u,v)]=cnt;
            head[u]=cnt++;
        }
    }Map;
    vector<int> f[100000];
    int dist[maxn][maxn];
    bool vis[maxn][maxn];
    int pre[maxn];
    void Dijstra(int s)
    {
        memset(dist,-1, sizeof(dist));
        pre[s]=s;
        priority_queue<pair<int,int>,vector<pair<int,int>>,greater<pair< int,int> > > q;
        for(int i=1;i<=n;i++) dist[s][i]=a[s];
        q.push(make_pair(a[s],s));
        while(!q.empty())
        {
            int u=q.top().second;
            q.pop();
            for(int i=1;i<=n;i++)
            {
                if(vis[u][i]||dist[u][i]==-1) continue;
                vis[u][i]=true;
                for(int j=Map.head[u];j!=-1;j=Map.edge[j].next)
                {
                    int v=Map.edge[j].v;
                    int x=mp[make_pair(u,v)];
                    bool flag=false;
                    for(int k=0;k<f[x].size();k++)
                    {
                        if(f[x][k]==i) flag=true;
                    }
                    if(!flag)
                    {
                        if((dist[v][u]>dist[u][i]+a[v]||dist[v][u]==-1)&&!vis[v][u])
                        {
                            dist[v][u]=dist[u][i]+a[v];
                            q.push(make_pair(dist[v][u],v));
                        }
                    }
                }
            }
        }
    }
    int main()
    {
        //freopen("in.txt","r",stdin);
        scanf("%d",&n);
        Map.init();
        for(int i=1;i<=n;i++)
        {
            int m;
            scanf("%d%d",&m,&a[i]);
            for(int j=1;j<=m;j++)
            {   int t,x,k;
                scanf("%d%d",&k,&x);
                Map.addedge(i,x);
                int tmp=mp[make_pair(i,x)];
                for(int l=1;l<=k;l++)
                {
                   int d;
                   scanf("%d",&d);
                   f[tmp].push_back(d);
                }
            }
        }
        Dijstra(1);
        int ans=1e9;
        for(int i=1;i<=n;i++)
        {
            if(dist[n][i]!=-1) ans=min(ans,dist[n][i]);
        }
        if(ans==1e9) printf("impossible
    ");
        else printf("%d
    ",ans);
        return 0;
    }
    

      

     
  • 相关阅读:
    java 简单的spring boot 服务器和文件上传接口(exceeds its maximum报错)
    nodeJs 远程服务器的部署和简单静态Web服务器
    Electron 搭建文件浏览器
    Electron项目的搭建和electron-builder打包
    Nexus搭建私服
    fiddler 抓包工具,抓取微信公众号直播回放视频; blob视频下载;微信视频号内容抓取
    二进制的原码,反码,补码,移码
    《RocketMQ源码系列》mq启动流程
    一条查询sql执行流程
    《springboot源码系列》——内置tomcat启动原理
  • 原文地址:https://www.cnblogs.com/zyf3855923/p/9745473.html
Copyright © 2011-2022 走看看