Sicily 1953. Hospital

1953. Hospital

Constraints

Time Limit: 1 secs, Memory Limit: 32 MB , Special Judge

Description

Given a directed graph G = (V, E), each node represents a city. In order to provide medical service in cities, a few hospitals need to be built in some of the cities. For some reasons, one hospital can just be put in one city, and two hospitals can not be put in two adjacent cities. Two cities i and j are considered adjacent if and only if there is an edge from i to j or from j to i. So there might be some cities have no hospitals in them, people of those cities can require the hospital to send doctors to their cities. Of course people will choose the city which is the closest to call. But if the closest city to travel is still far away, people would not satisfy with the quality of the medical service since much time would be taken on the travel. The dissatisfaction of people of the city which has no hospital can be measured as following:

S[i] = (number of edges from the closest hospital to city i / 3) * U

i is the city without hospital, and U is a constant integer, which will be given as input. And / means integer division. Your task is finding a plan of putting hospitals in cities so that the sum of dissatisfaction of the cities without hospitals is minimum. Note that you should make sure the service cover all cities, which means that there should no city can not be serviced.

Here is an example with four nodes. Supposed U is 10. If we put one hospital in node 4, so the node 1 will have a dissatisfaction of 10. A perfect plan is to put a hospital in node 2 and node 4.

Input

Input may consist of several test data sets. For each data set, it can be formatted as below:

First line comes with two integers, N and M, the number of nodes and edges. 1 <= N <= 100, 0 <= M <= 10000.

Then comes M lines, each line has two integers x and y, 1 <= x, y <= N, representing an edge from node x to node y, x ≠ y.

Finally comes an integer U, the constant number described as above. 1 <= U <= 500

Input is ended with N = M = 0.

Output

For each data set, first output one integer in one line, representing the minimum dissatisfaction of the optimal plan. Then output one integer in second line, representing the number of hospitals of your plan. Finally output all city numbers which has hospitals in one line in ascending order, separated with one blank space.

Sample Input

4 3
2 1
4 3
3 2
10
0 0

Sample Output

0
2

2 4

// Problem#: 1953
// Submission#: 3592158
// The source code is licensed under Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License
// URI: http://creativecommons.org/licenses/by-nc-sa/3.0/
// All Copyright reserved by Informatic Lab of Sun Yat-sen University
#include <stdio.h>
#include <string.h>

const int MAXN = 101;

char g[MAXN][MAXN], mark[MAXN], used[MAXN];
int n;

void dfs() {
    int i, j;
    for (i = 1; i <= n; i++) if (!mark[i]) break;
    if (i > n) return;
    mark[i] = 1;
    for (j = 1; j <= n; j++) if (g[i][j]) mark[j] = 1;
    dfs();
    for (j = 1; j <= n; j++) if (used[j] && g[j][i]) break;
    if (j > n) used[i] = 1;
}

int main() {
    int m, a, b, i;
    while (scanf("%d%d", &n, &m), n) {
        memset(g, 0, sizeof(g));
        while (m--) {
            scanf("%d%d", &a, &b);
            g[a][b] = 1;
        }
        scanf("%*d");
        memset(mark, 0, sizeof(mark));
        memset(used, 0, sizeof(used));
        dfs();
        for (i = 1, a = 0; i <= n; i++) if (used[i]) a++;
        printf("0\n");
        printf("%d\n", a);
        bool prn = false;
        for (i = 1; i <= n; i++) {
            if (used[i]) {
                if (prn) printf(" ");
                else prn = true;
                printf("%d", i);
            }
        }
        printf("\n");
    }
    return 0;
}                                 


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值