最大流最小费用-SPFA nowcoder H:Minimum-cost Flow

//#define LOCAL
#include 

using namespace std;
#define ll long long
#define mem(a, b) memset(a,b,sizeof(a))
#define INF 0x3f3f3f3f
#define DNF 0x7f
#define DBG printf("this is a input\n")
#define fi first
#define se second
#define mk(a, b) make_pair(a,b)
#define pb push_back
#define p_queue priority_queue
#define CLOSE ios::sync_with_stdio(0); cin.tie(0)
#define LF putchar('\n')
#define SP putchar(' ')


template
void read(T &x) {x = 0;char ch = getchar();ll f = 1;while(!isdigit(ch)){if(ch == '-')f *= -1;ch = getchar();}while(isdigit(ch)){x = x * 10 + ch - 48; ch = getchar();}x *= f;}
template
void read(T &first, Args& ... args) {read(first);read(args...);}
template
void write(T arg) {T x = arg;if(x < 0) {putchar('-'); x =- x;}if(x > 9) {write(x / 10);}putchar(x % 10 + '0');}
template
void write(T arg, Ts ... args) {write(arg);if(sizeof...(args) != 0) {putchar(' ');write(args ...);}}


void FileIO()
{
#ifdef LOCAL
    freopen("data.in", "r", stdin);
    freopen("odata.out", "w", stdout);
#endif
}
ll gcd(ll a, ll b) {
    return b == 0 ? a : gcd(b, a % b);
}

ll lcm(ll a, ll b) {
    return a / gcd(a, b) * b;
}

const int N = 105;
int n , m , s , t;
ll dis[N], fw[N], vis[N], pre[N];
int head[N], cnt;
vector  price;
ll ans[N];
struct eg{
    int from;
    int to;
    int next;
    ll c; //流量
    ll w; //费用
}edge[N * N];
void add(int f, int v , ll w, ll c)
{
    edge[cnt].from = f;
    edge[cnt].w = w;
    edge[cnt].to = v;
    edge[cnt].c = c;
    edge[cnt].next = head[f];
    head[f] = cnt ++;
}
void Init()
{
    mem(ans,0);
    mem(head,-1);
    price.clear();
    cnt = 0;
}
queue  q;
bool spfa()
{
    mem(dis,INF);
    mem(fw,INF);
    mem(vis,0);
    pre[s] = pre[t] = -1;
    q.push(s);
    vis[s] = 1;
    dis[s] = 0;
    while(!q.empty())
    {
        int u = q.front();
        q.pop();
        vis[u] = 0;
        for (int i = head[u] ; i != -1 ; i = edge[i].next)
        {
            int v = edge[i].to;
            ll w = edge[i].w, c = edge[i].c;
            if(edge[i].c && dis[u] + w < dis[v])
            {
                dis[v] = dis[u] + w;
                fw[v] = min(fw[u], c);
                pre[v] = i;
                if(vis[v] == 0)
                {
                    q.push(v);
                    vis[v] = 1;
                }
            }
        }
    }
    return pre[t] != -1;
}
void FYL()
{
    while(spfa())
    {

        price.pb(dis[t] * fw[t]);
        for (int i = pre[t] ; i != -1 ; i = pre[edge[i].from])
        {
            edge[i].c -= fw[t];
            edge[i^1].c -= fw[t];
        }
    }
    return ;
}
int main(void)
{
    while(scanf("%d %d",&n, &m) != EOF)
    {
        Init();
        for (int i = 1; i <= m ; i ++)
        {
            int u, v ;
            ll w;
            read(u,v,w);
            add(u,v,w,1);
            add(v,u,-w,0);
        }
        s = 1, t = n;
        FYL();
        int k = price.size();
        ans[0] = price[0];
        for (int i = 1 ; i < k ; i ++)
            ans[i] = ans[i-1] + price[i];
        int Q;
        read(q);
        while(Q --)
        {
            ll x , y;
            read(x,y);
            if(x == 0)
            {
                printf("NaN\n");
                continue;
            }
            ll flag = (y + x - 1) / x;
            if(flag > k)
                printf("NaN\n");
            else
            {
                ll z;
                if(y % x)
                    z = 1ll * x * ans[flag-2] + 1ll * (y%x) * price[flag-1];
                else
                    z = 1ll * ans[flag-1] * x;
                write(z/gcd(z,y));
                putchar('/');
                write(y/gcd(z,y));
            }
        }
    }
}

 

你可能感兴趣的:(网络流)