24h購物| | PChome| 登入
2012-06-02 06:49:49| 人氣811| 回應0 | 上一篇 | 下一篇

[UVA][sort] 10763 - Foreign Exchange

推薦 0 收藏 0 轉貼0 訂閱站台

Problem E
Foreign Exchange
Input: standard input
Output: standard output
Time Limit: 1 second

Your non-profit organization (iCORE - international Confederation of Revolver Enthusiasts) coordinates a very successful foreign student exchange program. Over the last few years, demand has sky-rocketed and now you need assistance with your task.

The program your organization runs works as follows: All candidates are asked for their original location and the location they would like to go to. The program works out only if every student has a suitable exchange partner. In other words, if a student wants to go from A to B, there must be another student who wants to go from B to A. This was an easy task when there were only about 50 candidates, however now there are up to 500000 candidates!

Input

The input file contains multiple cases. Each test case will consist of a line containing n - the number of candidates (1≤n≤500000), followed by n lines representing the exchange information for each candidate. Each of these lines will contain 2 integers, separated by a single space, representing the candidate's original location and the candidate's target location respectively. Locations will be represented by nonnegative integer numbers. You may assume that no candidate will have his or her original location being the same as his or her target location as this would fall into the domestic exchange program. The input is terminated by a case where n = 0; this case should not be processed.

 

Output

For each test case, print "YES" on a single line if there is a way for the exchange program to work out, otherwise print "NO".

 

Sample Input                               Output for Sample Input

10

1 2

2 1

3 4

4 3

100 200

200 100

57 2

2 57

1 2

2 1

10

1 2

3 4

5 6

7 8

9 10

11 12

13 14

15 16

17 18

19 20

0

 

YES

NO



對於 (a, b) 都找得到 (b, a) 去做配對,
我用了快排 O(nlogn) 然後用 O(n) 去做判斷, 但效率還不怎麼好

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <vector>
#include <queue>
using namespace std;
struct Arc {
    int to, v;
};
typedef vector<Arc>::iterator it;
vector<Arc> nlist[1001];
int dis[1001], dis2[1001];
int used[1001], used2[1001];
int ans = 0;
int spfa2(int, int);
void spfa(int st, int ed) {
    memset(dis, 63, sizeof(dis));
    memset(used, 0, sizeof(used));
    dis[st] = 0;
    queue<int> Q;
    Q.push(st);
    int tn;
    while(!Q.empty()) {
        tn = Q.front();
        used[tn] = 0;
        Q.pop();
        for(it i = nlist[tn].begin(); i != nlist[tn].end(); i++) {
            if(dis[i->to] > dis[tn] + i->v) {
                dis[i->to] = dis[tn] + i->v;
                if(used[i->to] == 0) {
                    used[i->to] = 1;
                    Q.push(i->to);
                }
            }
        }
    }
    Q.push(ed);
    int spath = dis[ed];
    while(!Q.empty()) {
        tn = Q.front();
        Q.pop();
        for(it i = nlist[tn].begin(), j; i != nlist[tn].end(); i++) {
            if(dis[tn] - i->v == dis[i->to]) {
                Q.push(i->to);
                for(j = nlist[i->to].begin(); j != nlist[i->to].end(); j++) {
                    if(j->to == tn && j->v == i->v) {
                        break;
                    }
                }
                if(i->v <= ans)
                    break;
                i->v <<= 1;
                j->v <<= 1;
                int tpath = spfa2(st, ed);
                if(tpath - spath > ans)
                    ans = tpath - spath;
                i->v >>= 1;
                j->v >>= 1;
                break;
            }
        }
    }
}
int spfa2(int st, int ed) {
    memset(dis2, 63, sizeof(dis2));
    memset(used2, 0, sizeof(used2));
    dis2[st] = 0;
    queue<int> Q2;
    Q2.push(st);
    int tn;
    while(!Q2.empty()) {
        tn = Q2.front();
        used2[tn] = 0;
        Q2.pop();
        for(it i = nlist[tn].begin(); i != nlist[tn].end(); i++) {
            if(dis2[i->to] > dis2[tn] + i->v) {
                dis2[i->to] = dis2[tn] + i->v;
                if(used2[i->to] == 0) {
                    used2[i->to] = 1;
                    Q2.push(i->to);
                }
            }
        }
    }
    return dis2[ed];
}
int main() {
    int n, m, i, a, b, w;
    while(scanf("%d %d", &n, &m) == 2) {
        for(i = 1; i <= n; i++)
            nlist[i].clear();
        Arc tmp;
        for(i = 0; i < m; i++) {
            scanf("%d %d %d", &a, &b, &w);
            tmp.to = b, tmp.v = w;
            nlist[a].push_back(tmp);
            tmp.to = a;
            nlist[b].push_back(tmp);
        }
        ans = 0;
        spfa(1, n);
        printf("%d\n", ans);
    }
    return 0;
}

台長: Morris
人氣(811) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 不分類 | 個人分類: UVA |
此分類下一篇:[UVA][倒水問題] 571 - Jugs
此分類上一篇:[UVA] 10714 - Ants

是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文