zoukankan      html  css  js  c++  java
  • 2015弱校联盟(2)

    J. Usoperanto
    Time Limit: 8000ms
    Memory Limit: 256000KB

    Usoperanto is an artificial spoken language designed and regulated by Usoperanto Academy. The academy is now in study to establish Strict Usoperanto, a variation of the language intended for formal documents.

    In Usoperanto, each word can modify at most one other word, and modifiers are always put before modifiees. For example, with a noun uso (“truth”) modified by an adjective makka (“total”), people say makka uso, not uso makka. On the other hand, there have been no rules about the order among multiple words modifying the same word, so in case uso is modified by one more adjective beta (“obvious”), people could say both makka beta uso and beta makka uso.

    In Strict Usoperanto, the word order will be restricted according to modification costs. Words in a phrase must be arranged so that the total modification cost is minimized. Each pair of a modifier and a modifiee is assigned a cost equal to the number of letters between the two words; the total modification cost is the sum of the costs over all modifier-modifiee pairs in the phrase. For example, the pair of makka and uso in a phrase makka beta uso has the cost of 4 for beta (four letters). As the pair of beta and uso has no words in between and thus the cost of zero, makka beta uso has the total modification cost of 4. Similarly beta makka uso has the total modification cost of 5. Applying the “minimum total modification cost” rule, makka beta uso is preferred to beta makka uso in Strict Usoperanto.

    Your mission in this problem is to write a program that, given a set of words in a phrase, finds the correct word order in Strict Usoperanto and reports the total modification cost.
    Input

    The format of the input is as follows.

    N
    M0 L0
    ...
    MN-1 LN-1
    

    The first line contains an integer N (1 ≤ N ≤ 106). N is the number of words in a phrase.

    Each of the following N lines contains two integers Mi (1 ≤ Mi ≤ 10) and Li (-1 ≤ Li ≤ N - 1, Li ≠ i) describing the i-th word (0 ≤ i ≤ N-1). Mi is the number of the letters in the word. Li specifies the modification: Li = -1 indicates it does not modify any word; otherwise it modifies the Li-th word.

    Note the first sample input below can be interpreted as the uso-beta-makka case.
    Output

    Print the total modification cost.
    Sample Input 1

    3
    3 -1
    4 0
    5 0

    Output for the Sample Input 1

    4

    Sample Input 2

    3
    10 -1
    10 0
    10 1

    Output for the Sample Input 2

    0

    Sample Input 3

    4
    1 -1
    1 0
    1 1
    1 0

    Output for the Sample Input 3

    1

    #include <bits/stdc++.h>
    #define LL long long
    using namespace std;
    const int Max = 1e6+100;
    vector<int>E[Max];
    int Len[Max];
    int to[Max];
    int num[Max],n;
    LL ans;
    bool cmp(int a,int b)
    {
        return Len[a]<Len[b];
    }
    void bfs()
    {
        queue<int>Q;
        memset(num,0,sizeof(num));
        for(int i=0;i<n;i++)
        {
            if(!E[i].size())//将没有被修饰词放进队列
            {
                Q.push(i);
            }
        }
        while(!Q.empty())
        {
            int u=Q.front();
            Q.pop();
            sort(E[u].begin(),E[u].end(),cmp);//将修饰词按长度从小到大排序,只有这样才能保证花费是最小的.如果修饰词也被修饰,则将他们看做一个整体,长度为他们的总长度.
            int len=E[u].size();
            for(int i=0,j=len-1;i<len;i++,j--)
            {
                Len[u]+=Len[E[u][i]];
                ans+=(Len[E[u][i]])*j;
            }
            if(to[u]!=-1)
            {
                num[to[u]]++;
                if(num[to[u]]==E[to[u]].size())//如果被修饰词的所有的修饰词计算完毕,就入队列
                {
                    Q.push(to[u]);
                }
            }
        }
    }
    int main()
    {
        int b;
        while(~scanf("%d",&n))
        {
            memset(E,0,sizeof(E));
            for(int i=0;i<n;i++)
            {
                scanf("%d %d",&Len[i],&b);
                to[i]=b;//记录自己所修饰的词的位置
                if(b!=-1)
                {
                    E[b].push_back(i);//建立边
                }
            }
            ans=0;
            bfs();
            cout<<ans<<endl;
        }
        return 0;
    }
    
    
  • 相关阅读:
    MVC5管道处理模型
    http协议下:为什么请求与响应会做到准确误的对应。不会出现请求与响应的错乱
    修改 IIS 队列长度
    修改 ASP.NET 请求队列的限制
    Windows性能查看器:系统的性能信息(I/O,IIS最大连接数,Sql) ,以及解决 asp.net IIS 一二百多用户并发
    详解 ASP.NET异步
    [C#]获得线程池中活动的线程数
    一个http请求就是一个线程吗,java的服务是每收到一个请求就新开一个线程来处理吗
    IIS连接数、IIS并发连接数、IIS最大并发工作线程数、应用程序池的队列长度、应用程序池的...
    ASP.NET MVC 线程和并发
  • 原文地址:https://www.cnblogs.com/juechen/p/5255908.html
Copyright © 2011-2022 走看看