uva1486 Transportation
2017-02-22 17:23
323 查看
There are N cities, and M directed roads connecting them. Now you
want to transport K units of goods from city 1 to city N . There are
many robbers on the road, so you must be very careful. The more goods
you carry, the more dangerous it is. To be more specific, for each
road i , there is a coefficient a i . If you want to carry x units of
goods along this road, you should pay a i x 2 dollars to hire guards
to protect your goods. And what’s worse, for each road i , there is an
upper bound C i , which means that you cannot transport more than C i
units of goods along this road. Please note you can only carry
integral unit of goods along each road. You should find out the
minimum cost to transport all the goods safely. Input There are
several test cases. The first line of each case contains three
integers, N , M and K . ( 1 N 100 , 1 M 5000 , 0 K 100 ).
Then M lines followed, each contains four integers ( u i ; v i ; a i ;
C i ) , indicating there is a directed road from city u i to v i ,
whose coefficient is a i and upper bound is C i . ( 1 u i ; v i N
, 0 < a i 100 , C i 5 ) Output Output one line for each test case,
indicating the minimum cost. If it is impossible to transport all the
K units of goods, output ‘
-1 ’
因为ci很小,直接拆成容量为1的ci条边,计算出每条边相应的权值。
want to transport K units of goods from city 1 to city N . There are
many robbers on the road, so you must be very careful. The more goods
you carry, the more dangerous it is. To be more specific, for each
road i , there is a coefficient a i . If you want to carry x units of
goods along this road, you should pay a i x 2 dollars to hire guards
to protect your goods. And what’s worse, for each road i , there is an
upper bound C i , which means that you cannot transport more than C i
units of goods along this road. Please note you can only carry
integral unit of goods along each road. You should find out the
minimum cost to transport all the goods safely. Input There are
several test cases. The first line of each case contains three
integers, N , M and K . ( 1 N 100 , 1 M 5000 , 0 K 100 ).
Then M lines followed, each contains four integers ( u i ; v i ; a i ;
C i ) , indicating there is a directed road from city u i to v i ,
whose coefficient is a i and upper bound is C i . ( 1 u i ; v i N
, 0 < a i 100 , C i 5 ) Output Output one line for each test case,
indicating the minimum cost. If it is impossible to transport all the
K units of goods, output ‘
-1 ’
因为ci很小,直接拆成容量为1的ci条边,计算出每条边相应的权值。
#include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int s=105,t=106,oo=0x3f3f3f3f,mod=107; int fir[110],ne[200010],to[200010],w[200010],c[200010], que[110],in[110],minw[110],len[110],fa[110], n,m,k,num; void add(int u,int v,int x,int y) { num++; ne[num*2]=fir[u]; fir[u]=num*2; to[num*2]=v; w[num*2]=x; c[num*2]=y; ne[num*2+1]=fir[v]; fir[v]=num*2+1; to[num*2+1]=u; w[num*2+1]=0; c[num*2+1]=-y; } void init() { int u,v,a,c; memset(fir,0,sizeof(fir)); num=0; while (m--) { scanf("%d%d%d%d",&u,&v,&a,&c); for (int i=1;i<=c;i++) add(u,v,1,a*i*i-a*(i-1)*(i-1)); } add(s,1,k,0); add(n,t,k,0); } void solve() { int u,v,hd,tl,ans=0; while (1) { hd=0,tl=1; in[s]=1; que[0]=s; memset(minw,0,sizeof(minw)); minw[s]=oo; memset(len,0x3f,sizeof(len)); len[s]=0; while (hd!=tl) { u=que[hd++]; hd%=mod; for (int i=fir[u];i;i=ne[i]) if (w[i]&&len[v=to[i]]>len[u]+c[i]) { len[v]=len[u]+c[i]; minw[v]=min(minw[u],w[i]); fa[v]=i; if (!in[v]) { in[v]=1; que[tl++]=v; tl%=mod; } } in[u]=0; } if (!minw[t]) break; for (int i=fa[t];i;i=fa[to[i^1]]) { w[i]-=minw[t]; w[i^1]+=minw[t]; } ans+=minw[t]*len[t]; } if (w[fir[s]]) printf("-1\n"); else printf("%d\n",ans); } int main() { while (scanf("%d%d%d",&n,&m,&k)==3) { init(); solve(); } }
相关文章推荐
- UVA 1486 - Transportation(网络流+拆边)
- (beginer) 网络流(最小费用流+费用跟流量有关) UVA 1486 - Transportation
- UVA-1486 - Transportation(最小费用最大流)
- uva 1486 Transportation (费用流+拆边)
- UVa1486 Transportation(最小费用最大流)
- Uva-1486-Transportation
- uva 1486 - Transportation(最小费用流)
- POJ1040 HDU1456 UVA301 UVALive5516 Transportation
- uva 301 Transportation 铁路公司的阳谋 纯dfs暴力
- UVa 301 - Transportation
- Transportation uva 301 为什么错了?
- uva 301 Transportation (DFS)
- UVA - 301 Transportation
- UVa 301 - Transportation 回溯
- UVa 301 - Transportation
- UVALive - 5095 Transportation(拆边+费用流)
- UVA 301 Transportation
- uva-301 - Transportation
- UVA 11228 - Transportation system.
- UVA_301_Transportation