CSU 1506 Double Shortest Paths(最小費用最大流)

1506: Double Shortest Paths

Time Limit: 1 Sec  Memory Limit: 128 MB
Submit: 338  Solved: 122
[Submit][Status][Web Board]

Description

Input

There will be at most 200 test cases. Each case begins with two integers n, m (1<=n<=500, 1<=m<=2000), the number of caves and passages. Each of the following m lines contains four integers u, v, di and ai (1<=u,v<=n, 1<=di<=1000, 0<=ai<=1000). Note that there can be multiple passages connecting the same pair of caves, and even passages connecting a cave and itself.

Output

For each test case, print the case number and the minimal total difficulty.

Sample Input

4 4
1 2 5 1
2 4 6 0
1 3 4 0
3 4 9 1
4 4
1 2 5 10
2 4 6 10
1 3 4 10
3 4 9 10

Sample Output

Case 1: 23

Case 2: 24

解題思路:這裏的邊可能有兩次費用,第一次走是di,第二次走是di+ai,所以這裏乾脆就兩節點之間建立兩條邊,容量都爲1,費用分別爲di和di+ai,然後再用超級源點與1號節點連接,容量爲2(走兩次),費用爲0,同理,超級匯點與n號節點連接,容量爲2。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<queue>
using namespace std;

const int maxn = 505;  
const int inf = 0x3f3f3f3f;  
struct Edge  
{  
    int from,to,next,flow,cost;  
}edge[10000];  
int n,m,cnt,head[maxn],pre[maxn];
int dis[maxn],st,ed;
bool inq[maxn];

void addedge(int u,int v,int flow,int cost)  
{  
    edge[cnt].from = u;  
    edge[cnt].to = v;  
    edge[cnt].flow = flow;  
    edge[cnt].cost = cost;  
    edge[cnt].next = head[u];  
    head[u] = cnt++;  
    swap(u,v);  
    edge[cnt].from = u;  
    edge[cnt].to = v;  
    edge[cnt].flow = 0;  
    edge[cnt].cost = -cost;  
    edge[cnt].next = head[u];  
    head[u] = cnt++;  
}  

int spfa(int s,int t)  
{  
    queue<int> q;  
    memset(dis,inf,sizeof(dis));  
    memset(inq,false,sizeof(inq));  
    memset(pre,-1,sizeof(pre)); //pre[i]表示最短路徑上以i爲終點的邊的編號  
    dis[s] = 0;  
    inq[s] = true;  
    q.push(s);  
    while(!q.empty())  
    {  
        int u = q.front();  
        q.pop();  
        inq[u] = false;  
        for(int i = head[u]; i != -1; i = edge[i].next)  
        {  
            int v = edge[i].to;  
            if(dis[v] > dis[u] + edge[i].cost && edge[i].flow > 0)  
            {  
                dis[v] = dis[u] + edge[i].cost;  
                pre[v] = i;  
                if(inq[v] == false)  
                {  
                    inq[v] = true;  
                    q.push(v);  
                }  
            }  
        }  
    }  
    return dis[t] != inf;  
}  

int MCMF(int s,int t)  
{  
    int mincost = 0,minflow,sumflow = 0; //最小費用,路徑中最小流量,總流量  
    while(spfa(s,t)) //找當前的最短路  
    {  
        minflow = inf;  
        for(int i = pre[t]; i != -1; i = pre[edge[i].from])  
            minflow = min(minflow,edge[i].flow);  
        sumflow += minflow;  
        for(int i = pre[t]; i != -1; i = pre[edge[i].from])  
        {  
            edge[i].flow -= minflow;  
            edge[i^1].flow += minflow;  
        }  
        mincost += dis[t] * minflow;  
    }  
    return mincost;  
}  

int main()
{
	int u,v,d,a,cas = 1;
	while(scanf("%d %d",&n,&m)!=EOF)
	{
		st = 0, ed = n + 1;
		cnt = 0;
		memset(head,-1,sizeof(head));
		for(int i = 1; i <= m; i++)
		{
			scanf("%d %d %d %d",&u,&v,&d,&a);
			addedge(u,v,1,d);
			addedge(u,v,1,d+a);
		}
		addedge(st,1,2,0);
		addedge(n,ed,2,0);
		printf("Case %d: %d\n",cas++,MCMF(st,ed));
	}
	return 0;
}


發佈了395 篇原創文章 · 獲贊 2 · 訪問量 14萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章