BNU 26480 Horror List【最短路】

鏈接:



Horror List

1000ms
65536KB
64-bit integer IO format: %lld      Java class name: Main
Font Size:  
Type:  

It was time for the 7th Nordic Cinema Popcorn Convention, and this year the manager Ian had a brilliant idea. In addition to the traditional film program, there would be a surprise room where a small group of people could stream a random movie from a large collection, while enjoying popcorn and martinis.

However, it turned out that some people were extremely disappointed, because they got to see movies like Ghosts of Mars, which instead caused them to tear out their hair in despair and horror.

To avoid this problem for the next convention, Ian has come up with a solution, but he needs your help to implement it. When the group enters the surprise room, they will type in a list of movies in a computer. This is the so-called horror list, which consists of bad movies that no one in the group would ever like to see. Of course, this list varies from group to group.

You also have access to the database Awesome Comparison of Movieswhich tells you which movies are directly similar to which. You can assume that movies that are similar to bad movies will be almost as bad. More specificly, we define the Horror index as follows:

Input

 

The first line of input contains three positive integers NHL (1H<N1000,0L10000), where N is the number of movies (represented by IDs, ranging from 0 to N1), H is the number of movies on the horror list and L is the number of similarities in the database.

The second line contains H unique space-separated integers xi (0xi<N) denoting the ID of the movies on the horror list.

The following L lines contains two space-separated integers ai,bi (0ai<bi<N), denoting that movie with ID ai is similar to movie with ID bi (and vice verca).

 

Output

Output the ID of the best movie in the collection (highest Horror Index). In case of a tie, output the movie with the lowest ID.

Sample Input

6 3 5
0 5 2
0 1
1 2
4 5
3 5
0 2

Sample Output

1

Source




算法:最短路 =_= !




題意+思路:


我等英語弱菜實在是傷不起Orz

有 N 場電影【編號從 0 到 N-1】 背景懶得寫了,說出來都是淚。。。
大致意思就是給你一張表上面有 H 個恐怖電影的標號。
然後給你  L  對關係,a, b 表示 a 和 b 相似。。。。【注意關係是雙向的,也就意味着是個無向圖】

然後題目的關鍵來了

根據上圖中的公式,求出每個電影的恐怖值。。。

PS:最終你會發現,最恐怖的電影的價值應該是 0, 最不恐怖的反而儘量大。。。這一點很容易搞反。


第一行 HI = 0 :表示在恐怖表中的電影,恐怖值均爲 0 ,而且不會被其他的關係定義覆蓋。【相當於以這些電影爲起點】

第二行 HI = Q+1:如果和當前電影類似的最恐怖的電影的恐怖值是 Q ,那麼當前電影的 HI = Q+1 

                            【相當於起點到當前電影的最小距離】

第三行 HI = INF :如果當前電影不和任何電影相似。【表示是孤立的點,最不恐怖的電影】


一般的大多是正常人,爲了不把自己嚇的抓狂,當然是選擇不怎麼恐怖的電影看了,這裏爲了儘量滿足大多數人的需求,當然是選擇最不恐怖的電影了。如果恐怖度一樣,就選擇編號最小的電影。


那麼題目就轉化成了求 HI 最大的,電影編號,如果有多個一樣,則輸出最小的。PS:如果有INF的,當然直接輸出最小的編號就over了


相當於以恐怖名單上的電影編號爲起點,d[index] = 0; 如果 index 出現在恐怖名單上【樣例中的第二行】

然後每一個點【電影】爲終點,求出起點集合到終點的最短距離。

最後輸出最短距離中距離最大的那個。。。


不知道說清楚了沒有,反正比賽的時候我是沒有想明白的,完了聽Orc 提別人說是最短路才反應過來。。。


code:

代碼醜了點Orz。。。。
不熟悉鄰接表的優先隊列實現還是個硬傷!!!
//PS:用鄰接矩陣寫的一個比較醜的代碼,還是不習慣用鄰接表寫。。。等熟悉了再折騰吧
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;

const int maxnN = 1000+10;
const int maxnL = 10000+10;
const int INF = 9999999;
int n,h,L;

int d[maxnN];
int w[maxnN][maxnN];
int vis[maxnN];

void Dijkstra()
{
    memset(vis, 0, sizeof(vis));
    for(int i = 0; i < n; i++)
    {
        int x , m = INF;
        for(int y = 0; y < n; y++) if(!vis[y] && d[y] <= m) m = d[x=y];
        if(m == INF) return; //如果存在孤立的點,不用繼續判斷
        vis[x] = 1;
        for(int y = 0; y < n; y++)
            d[y] = min(d[y], d[x]+w[x][y]);
    }
}
int main()
{
    while(scanf("%d%d%d", &n,&h,&L) != EOF)
    {
        for(int i = 0; i < n; i++) d[i] = INF; //初始化每一條邊,都沒有相鄰的

        for(int i = 0; i < n; i++)
            for(int j = 0; j < n; j++)
                w[i][j] = (i == j ? 0 : INF);
        int x;
        for(int i = 0; i < h; i++)
        {
            scanf("%d", &x);
            d[x] = 0; //在恐怖表中的電影
        }

        int a,b;
        for(int i = 0; i < L; i++)
        {
            scanf("%d%d", &a,&b);
            w[a][b] = 1; //雙向圖
            w[b][a] = 1;
        }
        Dijkstra();

        int Max = d[0]; //從第一個開始查找
        int index = 0;
        for(int i = 1; i < n; i++)
        {
            if(d[i] > Max)
            {
                Max = d[i];
                index = i;
            }
        }
        printf("%d\n", index);
        //for(int i = 0; i < n; i++) printf("%d ", d[i]); printf("\n");
    }
    return 0;
}




發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章