Lunch Time

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 617    Accepted Submission(s): 118


Problem Description
The campus of Nanjing University of Science and Technology can be viewed as a graph with N vertexes and M directed edges (vertexes are numbered from 0 to N - 1). Each edge has the same length 1. Every day, there are K students walking to the dinning-hall (vertex N - 1) from the teaching building (vertex 0) at lunch time. They all want reach the dinning-hall as soon as possible. However, each edge can only serve at most ci students at any time. Can you make arrangements for students, so that the last student can reach the dinning-hall as soon as possible? (It is assumed that the speed of the students is 1 edge per unit time)
 

Input
There are several test cases, please process till EOF.
The first line of each test case contains three integer N(2 <= N <= 2500), M(0 <= M <= 5000), K(0 <= K <= 10 9). Then follows M lines, each line has three numbers a i, b i, c i(0 <= c i <= 20), means there is an edge from vertex a i to b i with the capacity c i.
 

Output
For each test case, print an integer represents the minimum time. If the requirements can not be met, print “No solution”(without quotes) instead.
 

Sample Input
5 6 4 0 1 2 0 3 1 1 2 1 2 3 1 1 4 1 3 4 2 3 3 10 0 1 1 1 2 1 0 2 1 2 0 1
 

Sample Output
3 6 No solution
 

Source
 

Recommend
liuyiding   |   We have carefully selected several similar problems for you:   5901  5900  5899  5898  5897

题意:n个人从0点到第n-1点,每条边需要走1单位时间而且这条边单位时间内走的人数有上限,求每个人走最短距离到终点的总时间(每个人可能会等,但是不会绕远)

做法:

首先要理解好费用流的过程:把费用当做最短路的边权,对于残余网络(是叫这个名?就是可以增广的网络)跑spfa,因为费用流加的费用是对于单位流量而言的,那么总流量x费用就是最终的费用啦

对于这个题而言,如果把时间当做费用跑spfa之后就可以认为dist[dest]是这条路径的花费时间,记为dim[tot]

 ans+=(mid-dim[i])*tim[i]+tim[i];这个东西就是这条路径最多可以通过的人数,记为tim[tot]。

二分时   ans+=(mid-dim[i])*tim[i]+tim[i];  最终ans大于已知人数即可。注意二分l,mid,r,ans是long long 其他的int可以不改。

还有:spfaflow()==0或者没有可行流都可以判no sulotion


#include <iostream>
#include <cstdio>

using namespace std;

const int oo=1e9;//无穷大
const int maxm=100000;//边的最大数量,为原图的两倍
const int maxn=10000;//点的最大数量

int node,src,dest,edge;//node节点数,src源点,dest汇点,edge边数
int head[maxn],p[maxn],dis[maxn],q[maxn],vis[maxn];//head链表头,p记录可行流上节点对应的反向边,dis计算距离

struct edgenode
{
    int to;//边的指向
    int flow;//边的容量
    int cost;//边的费用
    int next;//链表的下一条边
} edges[maxm];

void prepare(int _node,int _src,int _dest);
void addedge(int u,int v,int f,int c);
bool spfa();

inline int min(int a,int b)
{
    return a<b?a:b;
}

inline void prepare(int _node,int _src,int _dest)
{
    node=_node;
    src=_src;
    dest=_dest;
    for (int i=0; i<node; i++)
    {
        head[i]=-1;
        vis[i]=false;
    }
    edge=0;
}

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

bool spfa()
{
    int i,u,v,l,r=0,tmp;
    for (i=0; i<node; i++) dis[i]=oo;
    dis[q[r++]=src]=0;
    p[src]=p[dest]=-1;
    for (l=0; l!=r; ((++l>=maxn)?l=0:1))
    {
        for (i=head[u=q[l]],vis[u]=false; i!=-1; i=edges[i].next)
        {
            if (edges[i].flow&&dis[v=edges[i].to]>(tmp=dis[u]+edges[i].cost))
            {
                dis[v]=tmp;
                p[v]=i^1;
                if (vis[v]) continue;
                vis[q[r++]=v]=true;
                if (r>=maxn) r=0;
            }
        }
    }
    return p[dest]>=0;
}
int dim[maxm],tim[maxm],tot;
int spfaflow()
{
    int i,ret=0,delta;
    tot=0;
    while (spfa())
    {
        //按记录原路返回求流量
        dim[tot]=dis[dest];
        tim[tot]=oo;
        for (i=p[dest]; i>=0; i=p[edges[i].to])
        {
            tim[tot]=min(tim[tot],edges[i^1].flow);
        }
        for (int i=p[dest]; i>=0; i=p[edges[i].to])
        {
            edges[i].flow+=tim[tot];
            edges[i^1].flow-=tim[tot];
        }
        ret+=delta*dis[dest];
        tot++;
    }
    return ret;
}
int main()
{
//    freopen("cin.txt","r",stdin);
    int n,m,k;
    while(~scanf("%d%d%d",&n,&m,&k))
    {
        prepare(n,0,n-1);
        while(m--)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
           // x--;y--;
            addedge(x,y,z);
        }
        spfaflow() ;
        if(k==0)
        {
            printf("0\n");
            continue;
        }
        if(tot==0)
        {
            printf("No solution\n");
            continue;
        }
        long long l=0,r=oo,mid,ans;
        int flag=0;
        while(l<=r)
        {
            mid=(l+r)/2;
            ans=0;
            for(int i=0;i<tot;i++)
            {
                if(dim[i]<=mid)
                    ans+=(mid-dim[i])*tim[i]+tim[i];
            }
            if(ans>=k)
            {
                flag=1;
                r=mid-1;
            }
            else l=mid+1;
        }
        if(flag==0)
        {
            printf("No solution\n");
            continue;
        }
        printf("%lld\n",l);
    }
    return 0;
}