..
#include <iostream>
#include <memory.h>
using namespace std;
const int maxn = 310;
const int maxe = 2048;
const int QSIZE = 1000000;
const bool db = false;
#define INSERT(x,y,z) p[top].j=y,p[top].value=z,p[top].next=head[x],head[x]=top++
struct data
{
int j, value, next;
};
struct queue
{
int a[QSIZE], head, tail;
queue():head(0), tail(0){}
bool empty(){return head == tail;}
void set_empty(){head=tail=0;}
int front(){return a[head];};
void pop(){head=(head+1)%QSIZE;};
void push(int x){a[tail]=x;tail=(tail+1)%QSIZE;}
}Q;
data p[maxe];
int head[maxn], top;
int Count[maxn], pre[maxn], E[maxn];
int f[maxn];
int V[maxn];
int n, m;
void ini()
{
memset(head, -1, sizeof(head));
top = 0;
}
int cnt = 0;
void Calc(int& a, int& b, int x)
{
int cnt = 0;
a=V[x];b=E[x];
int tmp = pre[x];
while (tmp != x)
{
cnt++;
if (cnt > 200) break;
a += V[tmp];
b += E[tmp];
tmp = pre[tmp];
}
}
bool Bellman_ford(int& a, int& b)
{
Q.set_empty();
Q.push(0);
memset(f, 63, sizeof(f));
memset(pre, -1, sizeof(pre));
memset(E, 0, sizeof(E));
memset(Count, 0, sizeof(0));
Count[0]=1;
f[0]=0;
while (!Q.empty())
{
int cur = Q.front();
Q.pop();
int i;
for (i = head[cur]; i != -1; i = p[i].next)
{
int next = p[i].j;
int next_f = f[cur] + a*p[i].value-b*V[next];
if (next_f < f[next])
{
f[next] = next_f;
E[next] = p[i].value;
Count[next] = Count[cur]+1;
Q.push(next);
if (pre[next] == cur)
{
pre[next] = cur;
Calc(a,b,next);
return true;
}
pre[next] = cur;
}
}
}
return false;
}
int main()
{
int i, j, k;
//freopen("input.txt", "r", stdin);
while (scanf("%d%d", &n, &m) == 2)
{
ini();
for (i = 0; i < n; i++)
scanf("%d", &V[i]);
for (i = 0; i < m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
x--,y--;
INSERT(x,y,z);
}
int a1 = 0, b1 = 1, a2 = 1, b2 = 0;
int cnt = 0;
while (a1!=a2 || b1!=b2)
{
int a = a1+a2, b = b1+b2;
printf("%d/%d %d/%d\n", a1, b1, a2, b2);
if (!Bellman_ford(a, b)) a2=a,b2=b;
else
a1=a,b1=b;
}
printf("%d %d\n", a1, b1);
}
return 0;
}
浙公网安备 33010602011771号