POJ_3068_Shortest_pair_of_paths_(最小费用流)

描述


http://poj.org/problem?id=3068

危险品:N个仓库由M条有向边连接,每条边都有一定费用。将两种危险品从0运到N-1,除了起点和终点外,危险品不能放在一起,也不能走相同的路径。求最小费用.

(好吧直接抄来的0.0)

 

"Shortest" pair of paths
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 1220   Accepted: 493

Description

A chemical company has an unusual shortest path problem.

There are N depots (vertices) where chemicals can be stored. There are M individual shipping methods (edges) connecting pairs of depots. Each individual shipping method has a cost. In the usual problem, the company would need to find a way to route a single shipment from the first depot (0) to the last (N - 1). That's easy. The problem they have seems harder. They have to ship two chemicals from the first depot (0) to the last (N - 1). The chemicals are dangerous and cannot safely be placed together. The regulations say the company cannot use the same shipping method for both chemicals. Further, the company cannot place the two chemicals in same depot (for any length of time) without special storage handling --- available only at the first and last depots. To begin, they need to know if it's possible to ship both chemicals under these constraints. Next, they need to find the least cost of shipping both chemicals from first depot to the last depot. In brief, they need two completely separate paths (from the first depot to the last) where the overall cost of both is minimal.

Your program must simply determine the minimum cost or, if it's not possible, conclusively state that the shipment cannot be made.

Input

The input will consist of multiple cases. The first line of each input will contain N and M where N is the number of depots and M is the number of individual shipping methods. You may assume that N is less than 64 and that M is less than 10000. The next M lines will contain three values, i, j, and v. Each line corresponds a single, unique shipping method. The values i and j are the indices of two depots, and v is the cost of getting from i to j. Note that these shipping methods are directed. If something can be shipped from i to j with cost 10, that says nothing about shipping from j to i. Also, there may be more than one way to ship between any pair of depots, and that may be important here.
A line containing two zeroes signals the end of data and should not be processed.

Output

follow the output format of sample output.

Sample Input

2 1
0 1 20
2 3
0 1 20
0 1 20
1 0 10
4 6
0 1 22
1 3 11
0 2 14
2 3 26
0 3 43
0 3 58
0 0

Sample Output

Instance #1: Not possible
Instance #2: 40
Instance #3: 73

Source

 

分析


每个边的容量都是1,增加一个源点与汇点,与他们相连的边容量是2,费用是0.然后跑最小费用流即可.

 

注意:

1.加爆了什么的...好吧只有我才会犯这种智障错(撞墙).

 

ps.人生第一道最小费用流的题.还有两个月就NOI了,感觉这节奏不死真难.

  1 #include<cstdio>
  2 #include<cstring>
  3 #include<vector>
  4 #include<queue>
  5 #include<algorithm>
  6 #define rep(i,n) for(int i=0;i<(n);i++)
  7 #define for1(i,a,n) for(int i=(a);i<=(n);i++)
  8 #define read(a) a=getnum()
  9 #define CC(i,a) memset(i,a,sizeof(i))
 10 using namespace std;
 11 
 12 const int maxn=64+5,INF=1<<27;
 13 int n,m;
 14 bool vis[maxn];
 15 int dis[maxn],prevv[maxn],preve[maxn];
 16 struct edge 
 17 {
 18     int to,cap,cost,rev;
 19     edge(){}
 20     edge(int a,int b,int c,int d):to(a),cap(b),cost(c),rev(d) {}
 21 };
 22 vector <edge> g[maxn];
 23 
 24 inline int getnum()
 25 {
 26     int r=0,k=1; char c;
 27     for(c=getchar();c<'0'||c>'9';c=getchar()) if(c=='-') k=-1;
 28     for(;c>='0'&&c<='9';c=getchar()) r=r*10+c-'0';
 29     return k*r;
 30 }
 31 
 32 void add_edge(int from,int to,int cap,int cost)
 33 {
 34     g[from].push_back(edge(to,cap,cost,g[to].size()));
 35     g[to].push_back(edge(from,0,-cost,g[from].size()-1));
 36 }
 37 
 38 void Spfa(int s)
 39 {
 40     for1(i,0,n+1) dis[i]=INF;
 41     dis[s]=0;
 42     CC(vis,0);
 43     queue <int> q;
 44     q.push(s);
 45     vis[s]=true;
 46     while(!q.empty())
 47     {
 48         int t=q.front(); q.pop();
 49         vis[t]=false;
 50         rep(i,g[t].size())
 51         {
 52             edge e=g[t][i];
 53             if(e.cap>0&&dis[e.to]-e.cost>dis[t])
 54             {
 55                 dis[e.to]=dis[t]+e.cost;
 56                 prevv[e.to]=t;
 57                 preve[e.to]=i;
 58                 if(!vis[e.to])
 59                 {
 60                     vis[e.to]=true;
 61                     q.push(e.to);
 62                 }
 63             }
 64         }
 65     }
 66 }
 67             
 68 
 69 int min_cost_flow(int s,int t,int f)
 70 {
 71     int res=0;
 72     while(f>0)
 73     {
 74         Spfa(s);
 75         if(dis[t]==INF) return -1;
 76         int d=f;
 77         for(int v=t;v!=s;v=prevv[v])
 78         {
 79             d=min(d,g[prevv[v]][preve[v]].cap);
 80         }
 81         f-=d;
 82         res+=d*dis[t];
 83         for(int v=t;v!=s;v=prevv[v])
 84         {
 85             edge &e=g[prevv[v]][preve[v]];
 86             e.cap-=d;
 87             g[v][e.rev].cap+=d;
 88         }
 89     }
 90     return res;
 91 }
 92 
 93 int main()
 94 {
 95 #ifndef ONLINE_JUDGE
 96     freopen("short.in","r",stdin);
 97     freopen("short.out","w",stdout);
 98 #endif
 99     int cnt=0;
100     while(scanf("%d%d",&n,&m)==2&&(n!=0||m!=0))
101     {
102         for1(i,1,m)
103         {
104             int from,to,cost;
105             read(from); read(to); read(cost);
106             from++; to++;
107             add_edge(from,to,1,cost);
108         }
109         add_edge(0,1,2,0);
110         add_edge(n,n+1,2,0);
111         printf("Instance #%d: ",++cnt);
112         int ans=min_cost_flow(0,n+1,2);
113         if(ans==-1)
114         {
115             printf("Not possible\n");
116         }
117         else
118         {
119             printf("%d\n",ans);
120         }
121         for1(i,0,n+1) g[i].clear();
122     }
123 #ifndef ONLINE_JUDGE
124     fclose(stdin);
125     fclose(stdout);
126     system("short.out");
127 #endif
128     return 0;
129 }
View Code

 

posted @ 2016-04-28 15:04  晴歌。  阅读(160)  评论(0编辑  收藏  举报