24h購物| | PChome| 登入
2013-08-28 07:16:14| 人氣2,693| 回應0 | 上一篇 | 下一篇

[UVA][dp] 1057 - Routing

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

As more and more transactions between companies and people are being carried out electronically over the Internet, secure communications have become an important concern. The Internet Cryptographic Protocol Company (ICPC) specializes in secure business-to-business transactions carried out over a network. The system developed by ICPC is peculiar in the way it is deployed in the network.

A network like the Internet can be modeled as a directed graph: nodes represent machines or routers, and edges correspond to direct connections, where data can be transmitted along the direction of an edge. For two nodes to communicate, they have to transmit their data along directed paths from the first node to the second, and from the second node to the first.

epsfbox{p3570.eps}

To perform a secure transaction, ICPC's system requires the installation of their software not only on the two endnodes that want to communicate, but also on all intermediate nodes on the two paths connecting the end-nodes. Since ICPC charges customers according to how many copies of their software have to be installed, it would be interesting to have a program that for any network and end-node pair finds the cheapest way to connect the nodes.

Input 

The input consists of several descriptions of networks. The first line of each description contains two integers N and M (2$ le$N$ le$100), the number of nodes and edges in the network, respectively. The nodes in the network are labeled 1, 2,..., N, where nodes 1 and 2 are the ones that want to communicate. The first line of the description is followed by M lines containing two integers X and Y (1$ le$X, Y$ le$N), denoting that there is a directed edge from X to Y in the network.

The last description is followed by a line containing two zeroes.

Output 

For each network description in the input, display its number in the sequence of descriptions. Then display the minimum number of nodes on which the software has to be installed, such that there is a directed path from node 1 to node 2 using only the nodes with the software, and also a path from node 2 to node 1 with the same property. (Note that a node can be on both paths but a path need not contain all the nodes.) The count should include nodes 1 and 2.

If node 1 and 2 cannot communicate, display `Impossible' instead.

Follow the format in the sample given below, and display a blank line after each test case.

Sample Input 

8 12
1 3 
3 4 
4 2 
2 5 
5 6 
6 1 
1 7 
7 1 
8 7 
7 8 
8 2 
2 8 

2 1 
1 2 

0 0

Sample Output 

Network 1 
Minimum number of nodes = 4 

Network 2 
Impossible

Claimer: The data used in this problem is unofficial data prepared by Shahriar Manzoor. So any mistake here does not imply mistake in the offcial judge data. Only Shahriar Manzoor is responsible for the mistakes. Report mistakes to Shahriar_manzoor@yahoo.com

在有向圖中,假設節點是主機,那麼圖中經過的部分都要安裝軟體才能進行傳輸工作,問從點 1 傳到點 2,再傳回點 1,問其中最少安裝軟體的主機,點可以重覆走。
-----
解法挺妙的,設定 dp[a][b] 表示為 1 ->a, b -> 1 的最少兩條路徑,然後依序增點嘗試,最後答案為 dp[2][2],但是路徑會回繞,因此特別考慮一種特殊型,使得 dp[b][a] = dp[a][b] + 造成 loop 的最小成本。


#include <stdio.h>
#include <string.h>
#include <queue>
#include <algorithm>
using namespace std;
int g[105][105];
int dp[105][105], inq[105][105];
int main() {
    //freopen("in.txt","r+t",stdin);
    //freopen("out2.txt","w+t",stdout);
    int n, m;
    int i, j, k;
    int x, y, cost;
    int cases = 0;
    while(scanf("%d %d", &n, &m) == 2 && n) {
        memset(inq, 0, sizeof(inq));
        for(i = 1; i <= n; i++) {
            for(j = 1; j <= n; j++) {
                g[i][j] = 0xfffffff;
            }
        }
        for(i = 0; i < m; i++) {
            scanf("%d %d", &x, &y);
            g[x][y] = 1;
        }
        for(k = 1; k <= n; k++)
            for(i = 1; i <= n; i++)
                for(j = 1; j <= n; j++)
                    g[i][j] = min(g[i][j], g[i][k]+g[k][j]);

        for(i = 1; i <= n; i++)
            for(j = 1; j <= n; j++)
                dp[i][j] = 0xfffffff;
        queue<int> X, Y;
        X.push(1), Y.push(1);
        dp[1][1] = 1;
        while(!X.empty()) {
            x = X.front(), X.pop();
            y = Y.front(), Y.pop();
            inq[x][y] = 0;
            //printf("%2d %2d %d\n", x, y, dp[x][y]);
            for(i = 1; i <= n; i++) {
                cost = (x != i && y != i);
                if(g[x][i] == 1) {
                    if(dp[i][y] > dp[x][y]+cost) {
                        dp[i][y] = dp[x][y]+cost;
                        if(inq[i][y] == 0) {
                            inq[i][y] = 1;
                            X.push(i), Y.push(y);
                        }
                    }
                }
                if(g[i][y] == 1) {
                    if(dp[x][i] > dp[x][y]+cost) {
                        dp[x][i] = dp[x][y]+cost;
                        if(inq[x][i] == 0) {
                            inq[x][i] = 1;
                            X.push(x), Y.push(i);
                        }
                    }
                }
            }
            if(x != y) {
                if(dp[y][x] > dp[x][y] + g[x][y]-1) {
                    dp[y][x] = dp[x][y] + g[x][y]-1;
                    if(inq[y][x] == 0) {
                        inq[y][x] = 1;
                        X.push(y), Y.push(x);
                    }
                }
            }
        }
        printf("Network %d\n", ++cases);
        if(dp[2][2] == 0xfffffff)
            puts("Impossible");
        else
            printf("Minimum number of nodes = %d\n", dp[2][2]);
        puts("");
    }
    return 0;
}
/*
11 46
2 4
3 11
10 4
11 11
3 10
9 9
9 7
10 1
6 8
4 9
11 1
6 2
3 9
1 11
5 3
9 6
8 1
3 6
6 1
4 6
4 10
4 4
3 3
7 10
7 6
1 9
7 9
1 8
9 10
4 5
5 2
10 10
1 3
8 9
1 4
5 4
1 7
2 11
5 5
5 11
11 8
10 2
8 4
6 3
9 3
7 1
*/

台長: Morris
人氣(2,693) | 回應(0)| 推薦 (0)| 收藏 (0)| 轉寄
全站分類: 教育學習(進修、留學、學術研究、教育概況) | 個人分類: UVA |
此分類下一篇:[UVA][bfs] 10097 - The Color Game
此分類上一篇:[UVA][bfs] 985 - Round and Round Maze

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