题目链接
#include
using namespace std;
typedef long long ll;
const int maxn = 1e2+5;
const int inf = 0x3f3f3f3f;
struct Edge{
int from,to,cap,flow,cost;
Edge(int u,int v,int c,int f,int w):from(u),to(v),cap(c),flow(f),cost(w){
};
};
vector<ll> res;
struct MCMF{
int n,m;
vector<Edge> edges;
vector<int> G[maxn];
int inq[maxn];
int d[maxn];
int p[maxn];
int a[maxn];
void init(int n){
this->n=n;
for(int i=0;i<n;i++)G[i].clear();
edges.clear();
}
void add(int from,int to,int cap,int cost){
edges.push_back(Edge(from,to,cap,0,cost));
edges.push_back(Edge(to,from,0,0,-cost));
m=edges.size();
G[from].push_back(m-2);
G[to].push_back(m-1);
}
bool Bellmanford(int s,int t,int &flow,ll &cost){
for(int i=0;i<n;i++)d[i]=inf;
memset(inq,0,sizeof(inq));
d[s]=0;inq[s]=1;p[s]=0;a[s]=inf;
queue<int> q;
q.push(s);
while(!q.empty()){
int u=q.front();q.pop();
inq[u]=0;
for(int i=0;i<G[u].size();i++){
Edge &e=edges[G[u][i]];
if(e.cap>e.flow&&d[e.to]>d[u]+e.cost){
d[e.to]=d[u]+e.cost;
p[e.to]=G[u][i];
a[e.to]=min(a[u],e.cap-e.flow);
if(!inq[e.to]){
q.push(e.to);
inq[e.to]=1;
}
}
}
}
if(d[t]==inf)return false;
flow+=a[t];
cost+=(ll)d[t]*(ll)a[t];
res.push_back(d[t]);
for(int u=t;u!=s;u=edges[p[u]].from){
edges[p[u]].flow+=a[t];
edges[p[u]^1].flow-=a[t];
}
return true;
}
int MincostMaxflow(int s,int t,ll &cost){
int flow=0;cost=0;
while(Bellmanford(s,t,flow,cost));
return flow;
}
}mcmf;
int main(){
int n,m;
while(~scanf("%d%d",&n,&m)){
mcmf.init(n+5);
res.clear();
for(int i=1;i<=m;i++){
int u,v,w;
scanf("%d%d%d",&u,&v,&w);
mcmf.add(u,v,1,w);
}
ll cost=0;
ll mf=mcmf.MincostMaxflow(1,n,cost);
int q;
scanf("%d",&q);
while(q--){
ll u,v;
scanf("%lld%lld",&u,&v);
if(mf*u<v){
puts("NaN");
continue;
}
ll sum=0,ans=0;
for(auto it:res){
if(sum+u<=v){
sum+=u;
ans+=it*u;
}else{
ans+=(v-sum)*it;
break;
}
}
ll g=__gcd(ans,v);
printf("%lld/%lld\n",ans/g,v/g);
}
}
return 0;
}