24h購物| | PChome| 登入
2014-02-17 21:07:32| 人氣1,710| 回應1 | 上一篇 | 下一篇

[UVA][最少費用流] 11823 - Two Longest Paths

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

Problem F

Two Longest Paths
Input: Standard Input

Output: Standard Output

Tekuhp is a tourist city. There are N intersections in the city, connected with M one-way roads. Each one-way road connects from some intersection to another. There maybe many roads that connect a pair of intersections. To make the city very amazing, the roads are constructed so that it is not possible to start at some intersection, travel along the roads, and return to the starting intersection. (It remains a strange secret how people of Tekuhp return home from work each day.)

There are two groups of tourists planning to visit the city. They want to travel along the roads from some intersection to another. However, both groups do not want to run into each other. So they want two paths P1 and P2, each Pi , for 1 <= i <= 2, starts at some intersection si and ends at intersection ti, such that both paths share no intersections, including the starting and the ending intersections. However, it is possible that a path Pi may contain only one node, i. e., si = ti.

Tourists also want to visit many places. Since you are a good planner, you want to maximize the total number of intersections in both path.

 

Input

           First line of the input contains an integer T (1 <= T <= 10), the number of test cases. After that T test cases follow.

Each test case starts with integers N and M (1 <= N <= 300; 1 <= M <= 3,000), where N denotes the number of intersections and M denotes the number of roads. The intersections are numbered from 1 to N. After that M lines, describing road connection, follow. Each line contains two integers A and B denoting that there is a one-way road from intersection A to intersection B.

 

Output

         The output must contain T lines, each line for each test case. For each test case, the output contains an integer L denoting the maximum number of intersections in two non-intersecting paths.

 

Sample Input

Output for Sample Input

3

3 2

1 2

2 3

8 9

1 2

2 3

3 4

5 6

6 7

7 8

2 6

6 3

3 7

4 2

1 2

3 4

3

8

4


Problemsetter: Jittat Fakcharoenphol

 

題目描述:

給一個有向無環的圖,求任意起點終點的兩條不相交的路徑,路徑上的點越多越好。

題目解法:

既然無環,可以使用最少費用流求解,多設兩個虛點到所有點的流量是 2。

由於每個點都只能用 1 次,因此要將每個點拆成入點和出點,中間的流量是 1。

#include <stdio.h>
#include <string.h>
#include <queue>
#include <deque>
using namespace std;
struct Node {
    int x, y, cap, cost;// x->y, v
    int next;
} edge[1000005];
int e, head[10005], dis[10005], prev[10005], record[10005], inq[10005];
void addEdge(int x, int y, int cap, int cost) {
    edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost;
    edge[e].next = head[x], head[x] = e++;
    edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost;
    edge[e].next = head[y], head[y] = e++;
}
int mincost(int s, int t) {
    int mncost = 0, flow, totflow = 0;
    int i, x, y;
    while(1) {
        memset(dis, 63, sizeof(dis));
        int oo = dis[0];
        dis[s] = 0;
        deque<int> Q;
        Q.push_front(s);
        while(!Q.empty()) {
            x = Q.front(), Q.pop_front();
            inq[x] = 0;
            for(i = head[x]; i != -1; i = edge[i].next) {
                y = edge[i].y;
                if(edge[i].cap > 0 && dis[y] > dis[x] + edge[i].cost) {
                    dis[y] = dis[x] + edge[i].cost;
                    prev[y] = x, record[y] = i;
                    if(inq[y] == 0) {
                        inq[y] = 1;
                        if(Q.size() && dis[Q.front()] > dis[y])
                            Q.push_front(y);
                        else
                            Q.push_back(y);
                    }
                }
            }
        }
        if(dis[t] == oo)
            break;
        flow = oo;
        for(x = t; x != s; x = prev[x]) {
            int ri = record[x];
            flow = min(flow, edge[ri].cap);
        }
        for(x = t; x != s; x = prev[x]) {
            int ri = record[x];
            edge[ri].cap -= flow;
            edge[ri^1].cap += flow;
            edge[ri^1].cost = -edge[ri].cost;
        }
        totflow += flow;
        mncost += dis[t] * flow;
    }
    return mncost;
}
int main() {
    int testcase;
    int n, m, x, y;
    int i, j, k;
    scanf("%d", &testcase);
    while(testcase--) {
        scanf("%d %d", &n, &m);
        memset(head, -1, sizeof(head));
        e = 0;
        int st = 2 * n, ed = 2 * n + 1;
        int t1 = 2 * n + 2, t2 = 2 * n + 3;
        for(i = 0; i < n; i++) {
            addEdge(2 * i, 2 * i + 1, 1, -1);
            addEdge(t1, 2 * i, 1, 0);
            addEdge(2 * i + 1, t2, 1, 0);
        }
        addEdge(st, t1, 2, 0);
        addEdge(t2, ed, 2, 0);
        for(i = 0; i < m; i++) {
            scanf("%d %d", &x, &y);
            x--, y--;
            addEdge(2 * x + 1, 2 * y, 1, 0);
        }
        printf("%d\n", -mincost(st, ed));
    }
    return 0;
}

台長: Morris
人氣(1,710) | 回應(1)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][五則運算、貪婪] 11890 - Calculus Simplified
此分類上一篇:[UVA] 11841 - Y-game

DJWS
你好,我想要將這段程式碼收錄到網站裡面!作為回報,剛好我知道下面這段程式碼有個簡潔的寫法:

void addEdge(int x, int y, int cap, int cost) {
edge[e].x = x, edge[e].y = y, edge[e].cap = cap, edge[e].cost = cost;
edge[e].next = head[x], head[x] = e++;
edge[e].x = y, edge[e].y = x, edge[e].cap = 0, edge[e].cost = -cost;
edge[e].next = head[y], head[y] = e++;
}

到了C++ TR1還是TR2,這段程式碼有個比較簡潔的寫法。

void addEdge(int x, int y, int cap, int cost) {
edge[e] = (Node){x, y, cap, cost, head[x]};
head[x] = e++;
edge[e] = (Node){y, x, 0, -cost, head[y]};
head[y] = e++;
}

到了C++11,還可以省略(Node)。

void addEdge(int x, int y, int cap, int cost) {
edge[e] = {x, y, cap, cost, head[x]};
head[x] = e++;
edge[e] = {y, x, 0, -cost, head[y]};
head[y] = e++;
}
2014-02-19 22:32:57
版主回應
恩恩
2014-03-07 13:00:31
是 (若未登入"個人新聞台帳號"則看不到回覆唷!)
* 請輸入識別碼:
請輸入圖片中算式的結果(可能為0) 
(有*為必填)
TOP
詳全文