hdu 3917 Road construction
1 /* 2 hdu 3917 Road constructions 最大權(quán)閉合圖 3 題意:n個城市 m個工程公司修路 每個公司要交稅收 有k個路段 分別花銷cost 4 當(dāng)如果選擇了某個公司,該公司負責(zé)的所有的路都要選,還有如果1->2的路由公司A負責(zé), 5 2->3的路由公司B負責(zé),那么如果選了公司A就必須選公司B, 求最大的收益 6 建圖:稅收為正權(quán)值、cost為負權(quán)值 start連所有公司m end連所有公司m 7 規(guī)則要求聯(lián)系的公司之間為INF eg addedge(a,b,INF);該處解決方法建結(jié)構(gòu)體詳見具體代碼 8 start=0;end=1+m;totals=end+1; 9 建圖完成 10 答案:最大權(quán)閉合圖。一模一樣的。答案為稅收總和-最大流 11 */ 12 #include <iostream> 13 #include<cstdio> 14 #include<cstring> 15 #include<vector> 16 #include<queue> 17 #include<algorithm> 18 using namespace std; 19 #define min(a,b) ((a)<(b))?(a):(b) 20 #define max(a,b) ((a)>(b))?(a):(b) 21 #define MAXN 6005//盡量開大點 22 #define MAXM 100002 //M取N的平方倍或者N*9 23 #define INF 0x3f3f3f3f 24 int m,n; 25 struct node 26 { 27 int n;//點編號 28 int w;//點權(quán) 29 }Node[MAXM];//n變m 30 //鏈?zhǔn)角跋蛐?/span> 31 struct s//存工程每個工程公司修路的起點終點 32 { 33 int s;//起點 34 int t;//終點 35 int c;//該公司名稱 36 }ss[MAXN]; 37 struct enode 38 { 39 int t; 40 int w; //權(quán)值 41 int c; //流量 42 // int cost; 43 // int pre; //前向指針 44 int next; 45 }; 46 47 struct enode e[MAXM]; 48 int box[MAXN],ecnt; 49 //int etail[MAXN]; //尾部 50 void init() 51 { 52 ecnt=0; 53 memset(box,-1,sizeof(box)); 54 // memset(etail,-1,sizeof(etail)); //初始化尾部 55 } 56 void addedge(int f,int t,int c) //流量重載 57 { 58 e[ecnt].next=box[f]; 59 e[ecnt].t=t; 60 e[ecnt].c=c; 61 box[f]=ecnt++; 62 e[ecnt].next=box[t]; 63 e[ecnt].t=f; 64 e[ecnt].c=0; 65 box[t]=ecnt++; 66 } 67 int sap(int s,int t,int N)//最大流問題 68 { 69 int gap[MAXN],lvl[MAXN],cur[MAXN],pre[MAXN]; 70 int curflow,ans=0,u,tmp,neck,i; 71 memset(lvl,0,sizeof(lvl)); 72 memset(gap,0,sizeof(gap)); 73 memset(pre,-1,sizeof(pre)); 74 for(i=0;i<N;i++) 75 cur[i]=box[i]; 76 gap[0]=N; 77 u=s; 78 while(lvl[s]<N) 79 { 80 if(u==t) 81 { 82 curflow=INF; 83 for(i=s;i!=t;i=e[cur[i]].t) 84 { 85 if(curflow>e[cur[i]].c) 86 { 87 neck=i; 88 curflow=e[cur[i]].c; 89 } 90 } 91 for(i=s;i!=t;i=e[cur[i]].t) 92 { 93 tmp=cur[i]; 94 e[tmp].c-=curflow; 95 e[tmp^1].c+=curflow; 96 } 97 ans+=curflow; 98 u=neck; 99 } 100 for(i=cur[u];i!=-1;i=e[i].next) 101 if(e[i].c && lvl[u]==lvl[e[i].t]+1) 102 break; 103 if(i!=-1) 104 { 105 cur[u]=i; 106 pre[e[i].t]=u; 107 u=e[i].t; 108 } 109 else 110 { 111 if(--gap[lvl[u]]==0) 112 break; 113 cur[u]=box[u]; 114 for(tmp=N,i=box[u];i!=-1;i=e[i].next) 115 if(e[i].c) 116 tmp=min(tmp,lvl[e[i].t]); 117 lvl[u]=tmp+1; 118 gap[lvl[u]]++; 119 if(u!=s) 120 u=pre[u]; 121 } 122 } 123 return ans; 124 } 125 int index[5010]; 126 int main() 127 { 128 int a,b,c,d,t,k,w,ct; 129 int i,j; 130 int start,end,ans; 131 while(scanf("%d%d",&n,&m)&&n&&m) //; 132 { 133 // if(m==0&&n==0)break; 134 start=ans=ct=0; 135 end=1+m;//終點編號 136 init(); 137 memset(index,0,sizeof(index)); 138 for(i=1;i<=m;i++) 139 { 140 scanf("%d",&w); 141 addedge(start,i,w);//起點連公司 稅收為正值 142 ans+=w;//正權(quán)值累加 143 } 144 scanf("%d",&k); 145 for(i=1;i<=k;i++) 146 { 147 scanf("%d%d%d%d",&a,&b,&c,&d); 148 ss[i].s=a; ss[i].t=b; 149 ss[i].c=c; 150 index[c]+=d;//對應(yīng)工程公司付費求和 減小建邊次數(shù) 151 //addedge(c,end,d); 152 } 153 for(i=1;i<=m;i++) 154 { 155 addedge(i,end,index[i]);//公司連終點 156 } 157 for(i=1;i<=k;i++)//k個公路條件 158 { 159 for(j=1;j<=k;j++) 160 { 161 if(ss[i].t==ss[j].s&&(i!=j)&&ss[i].c!=ss[j].c) 162 { 163 addedge(ss[i].c,ss[j].c,INF); 164 } 165 } 166 } 167 168 t=sap( start,end,end+1); 169 printf("%d\n",ans-t); 170 } 171 return 0; 172 } 173 /* 174 4 2 175 500 10 176 4 177 1 2 1 10 178 2 3 1 20 179 4 3 1 30 180 1 4 2 60 181 4 2 182 500 100 183 5 184 1 2 1 10 185 2 3 1 20 186 4 3 1 30 187 4 3 2 10 188 1 4 2 60 189 3 1 190 10 191 3 192 1 2 1 100 193 2 3 1 100 194 3 1 1 100 195 0 0 196 197 198 199 Sample Output 200 201 440 202 470 203 0 204 */
//http://acm.hdu.edu.cn/webcontest/contest_showproblem.php?cid=4456&pid=1010&ojid=0
posted on 2013-04-22 21:18 ACM_Someone like you 閱讀(360) 評論(3) 收藏 舉報
浙公網(wǎng)安備 33010602011771號