Poj2749 Building Roads

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/Elemmir/article/details/51559185

有N个点,每个点向S1或S2连边。有一些恩怨情仇限制某些对点不能同时连向同一点,而某些对点必须同时连向同一点。每个点到S1和S2都有距离,我们要求可行方案中最大的两点间通过所连的S1(S2)到达彼此的距离最小是多少。

除了恩怨情仇外的限制再加上二分出的距离的限制来做2-SAT判定就好了。

#include<cmath>
#include<stack>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<iostream>
using namespace std;

struct edge {
    int x,y,next;
    edge(){}
    edge(int _x,int _y,int _nt):x(_x),y(_y),next(_nt){}
} e[ 1000005 ];
int head[2000],tot = 0;
inline void addedge(int x,int y) {
    e[++tot] = edge(x,y,head[x]); head[x] = tot;
}
bool ins[2000];
int inc[2000],dfn[2000],low[2000],SCC,T;
stack<int>sta;
void tarjan(int x){
    dfn[x] = low[x] = ++T;
    sta.push(x); ins[x] = 1;
    for (int i = head[x]; i; i = e[i].next) {
        int y = e[i].y;
        if( ! dfn[y] ) {
            tarjan(y);
            low[x] = min(low[x], low[y]);
        }else if( ins[y] && dfn[y] < low[x]) low[x] = dfn[y];
    }
    if( low[x] == dfn[x] ){
        SCC++;
        while(1) {
            int y = sta.top(); sta.pop(); ins[y] = 0;
            inc[y] = SCC;
            if (x == y ) break;
        }
    }
}
struct point {
    int x,y;
    point(){}
    point(int _x,int _y):x(_x),y(_y){}
} a[2005],s[3];
int dis(point a,point b) {
    return abs(a.x-b.x) + abs(a.y-b.y);
}
int N,A,B;
int e1[2005][2],e2[2005][2];
int d1[2005],d2[2005],d12;
bool judge(int d) {
    memset(head,0,sizeof head);
    tot = 0;

    for (int i = 1; i <= A; i++) {
        addedge(e1[i][0],   e1[i][1]+N);
        addedge(e1[i][0]+N, e1[i][1]);
        addedge(e1[i][1],   e1[i][0]+N);
        addedge(e1[i][1]+N, e1[i][0]);
    }
    for (int i = 1; i <= B; i++) {
        addedge(e2[i][0],   e2[i][1]);
        addedge(e2[i][0]+N, e2[i][1]+N);
        addedge(e2[i][1],   e2[i][0]);
        addedge(e2[i][1]+N, e2[i][0]+N);
    }
    for (int i = 1; i <= N; i++) for (int j = i+1; j <= N; j++) {
        if ( d1[i] + d1[j] > d ) {
            addedge(i,j+N);         
            addedge(j,i+N);
        }
        if ( d2[i] + d2[j] > d ) {
            addedge(i+N,j);
            addedge(j+N,i);
        } 
        if ( d1[i] + d12 + d2[j] > d ) {
            addedge(i,j); addedge(j+N,i+N);
        }
        if ( d2[i] + d12 + d1[j] > d ) {
            addedge(i+N,j+N); addedge(j,i);
        } 
    }
    SCC = 0; T = 0;
    memset(ins,0,sizeof ins);
    memset(dfn,0,sizeof dfn);
    for (int i = 1; i <= N<<1; i++) if( ! dfn[i] ) tarjan(i);
    for (int i = 1; i <= N; i++)
        if( inc[i] == inc[i+N] ) return 0;
    return 1;
}
int main() {
    scanf("%d%d%d",&N,&A,&B);
    scanf("%d%d%d%d",&s[1].x,&s[1].y,&s[2].x,&s[2].y);
    int L = 0x3f3f3f3f,R = -1 ,mid,ans;
    d12 = dis(s[1],s[2]);
    for (int i = 1; i <= N; i++) {
        scanf("%d%d",&a[i].x,&a[i].y);
        d1[i] = dis(a[i], s[1]);
        d2[i] = dis(a[i], s[2]);
        L = min(L,min(d1[i],d2[i]));
        R = max(R,max(d1[i],d2[i]));
    }
    L <<= 1, (R<<=1)+= d12, ans = 0x3f3f3f3f;
    for (int i = 1; i <= A; i++) scanf("%d%d",&e1[i][0],&e1[i][1]);
    for (int i = 1; i <= B; i++) scanf("%d%d",&e2[i][0],&e2[i][1]);
    while( L <= R) {
        int mid = (L + R) >> 1;
        if( judge(mid) ) ans = mid,R=mid-1;
        else L = mid + 1;
    }
    if( ans == 0x3f3f3f3f ) ans = -1;
    printf("%d\n",ans);
}
阅读更多
相关热词

Building roads

05-16

Problem DescriptionnFarmer John's farm has N barns, and there are some cows that live in each barn. The cows like to drop around, so John wants to build some roads to connect these barns. If he builds roads for every pair of different barns, then he must build N * (N - 1) / 2 roads, which is so costly that cheapskate John will never do that, though that's the best choice for the cows. nnClever John just had another good idea. He first builds two transferring point S1 and S2, and then builds a road connecting S1 and S2 and N roads connecting each barn with S1 or S2, namely every barn will connect with S1 or S2, but not both. So that every pair of barns will be connected by the roads. To make the cows don't spend too much time while dropping around, John wants to minimize the maximum of distances between every pair of barns. nnThat's not the whole story because there is another troublesome problem. The cows of some barns hate each other, and John can't connect their barns to the same transferring point. The cows of some barns are friends with each other, and John must connect their barns to the same transferring point. What a headache! Now John turns to you for help. Your task is to find a feasible optimal road-building scheme to make the maximum of distances between every pair of barns as short as possible, which means that you must decide which transferring point each barn should connect to. nnWe have known the coordinates of S1, S2 and the N barns, the pairs of barns in which the cows hate each other, and the pairs of barns in which the cows are friends with each other. nnNote that John always builds roads vertically and horizontally, so the length of road between two places is their Manhattan distance. For example, saying two points with coordinates (x1, y1) and (x2, y2), the Manhattan distance between them is |x1 - x2| + |y1 - y2|. nn nnInputnThe first line of input consists of 3 integers N, A and B (2 <= N <= 500, 0 <= A <= 1000, 0 <= B <= 1000), which are the number of barns, the number of pairs of barns in which the cows hate each other and the number of pairs of barns in which the cows are friends with each other. nnNext line contains 4 integer sx1, sy1, sx2, sy2, which are the coordinates of two different transferring point S1 and S2 respectively. nnEach of the following N line contains two integer x and y. They are coordinates of the barns from the first barn to the last one. nnEach of the following A lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows hate each other. nnThe same pair of barns never appears more than once. nnEach of the following B lines contains two different integers i and j(1 <= i < j <= N), which represent the i-th and j-th barns in which the cows are friends with each other. The same pair of barns never appears more than once. nnYou should note that all the coordinates are in the range [-1000000, 1000000]. n nnOutputnYou just need output a line containing a single integer, which represents the maximum of the distances between every pair of barns, if John selects the optimal road-building scheme. Note if there is no feasible solution, just output -1. n nnSample Inputn4 1 1n12750 28546 15361 32055n6706 3887n10754 8166n12668 19380n15788 16059n3 4n2 3n nnSample Outputn53246

换一批

ROADS

10-28

DescriptionnnN cities named with numbers 1 ... N are connected with one-way roads. Each road has two parameters associated with it : the road length and the toll that needs to be paid for the road (expressed in the number of coins). nBob and Alice used to live in the city 1. After noticing that Alice was cheating in the card game they liked to play, Bob broke up with her and decided to move away - to the city N. He wants to get there as quickly as possible, but he is short on cash. nnWe want to help Bob to find the shortest path from the city 1 to the city N that he can afford with the amount of money he has. nInputnnThe first line of the input contains the integer K, 0 <= K <= 10000, maximum number of coins that Bob can spend on his way. nThe second line contains the integer N, 2 <= N <= 100, the total number of cities. nnThe third line contains the integer R, 1 <= R <= 10000, the total number of roads. nnEach of the following R lines describes one road by specifying integers S, D, L and T separated by single blank characters : nS is the source city, 1 <= S <= N nD is the destination city, 1 <= D <= N nL is the road length, 1 <= L <= 100 nT is the toll (expressed in the number of coins), 0 <= T <=100nnNotice that different roads may have the same source and destination cities.nOutputnnThe first and the only line of the output should contain the total length of the shortest path from the city 1 to the city N whose total toll is less than or equal K coins. nIf such path does not exist, only number -1 should be written to the output. nSample Inputnn5n6n7n1 2 2 3n2 4 3 3n3 4 2 4n1 3 4 1n4 6 2 1n3 5 2 0n5 4 3 2nSample Outputnn11

Jungle Roads Jungle Roads

09-22

Problem DescriptionrnrnrnThe Head Elder of the tropical island of Lagrishan has a problem. A burst of foreign aid money was spent on extra roads between villages some years ago. But the jungle overtakes roads relentlessly, so the large road network is too expensive to maintain. The Council of Elders must choose to stop maintaining some roads. The map above on the left shows all the roads in use now and the cost in aacms per month to maintain them. Of course there needs to be some way to get between all the villages on maintained roads, even if the route is not as short as before. The Chief Elder would like to tell the Council of Elders what would be the smallest amount they could spend in aacms per month to maintain roads that would connect all the villages. The villages are labeled A through I in the maps above. The map on the right shows the roads that could be maintained most cheaply, for 216 aacms per month. Your task is to write a program that will solve such problems. rnrnThe input consists of one to 100 data sets, followed by a final line containing only 0. Each data set starts with a line containing only a number n, which is the number of villages, 1 < n < 27, and the villages are labeled with the first n letters of the alphabet, capitalized. Each data set is completed with n-1 lines that start with village labels in alphabetical order. There is no line for the last village. Each line for a village starts with the village label followed by a number, k, of roads from this village to villages with labels later in the alphabet. If k is greater than 0, the line continues with data for each of the k roads. The data for each road is the village label for the other end of the road followed by the monthly maintenance cost in aacms for the road. Maintenance costs will be positive integers less than 100. All data fields in the row are separated by single blanks. The road network will always allow travel between all the villages. The network will never have more than 75 roads. No village will have more than 15 roads going to other villages (before or after in the alphabet). In the sample input below, the first data set goes with the map above. rnrnThe output is one integer per line for each data set: the minimum cost in aacms per month to maintain a road system that connect all the villages. Caution: A brute force solution that examines every possible set of roads will not finish within the one minute time limit. rn rnrnSample Inputrn9rnA 2 B 12 I 25rnB 3 C 10 H 40 I 8rnC 2 D 18 G 55rnD 1 E 44rnE 2 F 60 G 38rnF 0rnG 1 H 35rnH 1 I 35rn3rnA 2 B 10 C 40rnB 1 C 20rn0rn rnrnSample Outputrn216rn30rn rnrnSourcernMid-Central USA 2002

没有更多推荐了,返回首页