程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> C語言 >> C++ >> 關於C++ >> CSU 1506 Problem D: Double Shortest Paths(最小費用最大流)

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

編輯:關於C++

題意:2個人從1走到n,如果一條路第一次走則是價值di,如果第二次還走這條路則需要價值di+ai,要你輸出2個人到達終點的最小價值!

太水了!一條邊建2次就OK了!第一次價值為di,第二次為ai+di,添加源點匯點跑最小費用最大流就OK了!

AC代碼:

 

#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
#include
using namespace std;
#define CL(x,v); memset(x,v,sizeof(x));
#define INF 0x3f3f3f3f
#define LL long long
#define REP(i,r,n) for(int i=r;i<=n;i++)
#define RREP(i,n,r) for(int i=n;i>=r;i--)

int sumFlow;
const int MAXN = 600+10;
const int MAXM = 4000+10;
struct Edge
{
    int u, v, cap, cost;
    int next;
}edge[MAXM<<2];

int NE;
int head[MAXN], dist[MAXN], pp[MAXN];
bool vis[MAXN];

void init()
{
    NE = 0;
    memset(head, -1, sizeof(head));
}
void addedge(int u, int v, int cap, int cost)
{
    edge[NE].u = u;
    edge[NE].v = v;
    edge[NE].cap = cap;
    edge[NE].cost = cost;
    edge[NE].next = head[u];
    head[u] = NE++;

    edge[NE].u = v;
    edge[NE].v = u;
    edge[NE].cap = 0;
    edge[NE].cost = -cost;
    edge[NE].next = head[v];
    head[v] = NE++;
}
bool SPFA(int s, int t, int n)
{
    int i, u, v;
    queue  qu;
    memset(vis,false,sizeof(vis));
    memset(pp,-1,sizeof(pp));
    for(i = 0; i <= n; i++) dist[i] = INF;
    vis[s] = true; dist[s] = 0;
    qu.push(s);
    while(!qu.empty())
    {
        u = qu.front(); qu.pop(); vis[u] = false;
        for(i = head[u]; i != -1; i = edge[i].next)
        {
            v = edge[i].v;
            if(edge[i].cap && dist[v] > dist[u] + edge[i].cost)
            {
                dist[v] = dist[u] + edge[i].cost;
                pp[v] = i;
                if(!vis[v])
                {
                    qu.push(v);
                    vis[v] = true;
                }
            }
        }
    }
    if(dist[t] == INF) return false;
    return true;
}
int MCMF(int s, int t, int n)
{
    int flow = 0;
    int i, minflow, mincost;
    mincost = 0;
    while(SPFA(s, t, n))
    {
        minflow = INF + 1;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
            if(edge[i].cap < minflow)
                minflow = edge[i].cap;
        flow += minflow;
        for(i = pp[t]; i != -1; i = pp[edge[i].u])
        {
            edge[i].cap -= minflow;
            edge[i^1].cap += minflow;
        }
        mincost += dist[t] * minflow;
    }
    sumFlow = flow;
    return mincost;
}
int main()
{
    int n,m;
    int cas=1;
    while(~scanf(%d%d,&n,&m))
    {
        init();

        int s=0;
        int t=n+1;

        for(int i=0;i

 

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved