A - Currency Exchange
Several currency exchange points are working in our city. Let us suppose that each point specializes in two particular currencies and performs exchange operations only with these currencies. There can be several points specializing in the same pair of currencies. Each point has its own exchange rates, exchange rate of A to B is the quantity of B you get for 1A. Also each exchange point has some commission, the sum you have to pay for your exchange operation. Commission is always collected in source currency.
For example, if you want to exchange 100 US Dollars into Russian Rubles at the exchange point, where the exchange rate is 29.75, and the commission is 0.39 you will get (100 - 0.39) * 29.75 = 2963.3975RUR.
You surely know that there are N different currencies you can deal with in our city. Let us assign unique integer number from 1 to N to each currency. Then each exchange point can be described with 6 numbers: integer A and B - numbers of currencies it exchanges, and real R AB, C AB, R BA and C BA - exchange rates and commissions when exchanging A to B and B to A respectively.
Nick has some money in currency S and wonders if he can somehow, after some exchange operations, increase his capital. Of course, he wants to have his money in currency S in the end. Help him to answer this difficult question. Nick must always have non-negative sum of money while making his operations.
Input
The first line of the input contains four numbers: N - the number of currencies, M - the number of exchange points, S - the number of currency Nick has and V - the quantity of currency units he has. The following M lines contain 6 numbers each - the description of the corresponding exchange point - in specified above order. Numbers are separated by one or more spaces. 1<=S<=N<=100, 1<=M<=100, V is real number, 0<=V<=10 3.
For each point exchange rates and commissions are real, given with at most two digits after the decimal point, 10 -2<=rate<=10 2, 0<=commission<=10 2.
Let us call some sequence of the exchange operations simple if no exchange point is used more than once in this sequence. You may assume that ratio of the numeric values of the sums at the end and at the beginning of any simple sequence of the exchange operations will be less than 10 4.
Output
If Nick can increase his wealth, output YES, in other case output NO to the output file.
Sample Input
3 2 1 20.0 1 2 1.00 1.00 1.00 1.00 2 3 1.10 1.00 1.10 1.00
Sample Output
YES
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<queue>
using namespace std;
struct edge {
int to;
double r,c;
int nxt;
} e[201];
int head[201];
double dis[201];
int vis[201];
int num[201];
int m,n,s;
double v;
int cnt;
void map(int a,int b,double r,double c) { //邻接表-链式前向星
e[cnt].to=b;
e[cnt].r=r;
e[cnt].c=c;
e[cnt].nxt=head[a];
head[a]=cnt++;
}
int spfa() {
memset(vis,0,sizeof(vis));
memset(num,0,sizeof(num));
queue<int>qu;
dis[s]=v;
vis[s]=1;
num[s]++;
qu.push(s);
while(qu.size()) {
int q = qu.front();
qu.pop();
vis[q]=0;
for(int i=head[q]; i!=-1; i=e[i].nxt) {
int p = e[i].to;
if(dis[p]<(dis[q]-e[i].c)*e[i].r) {
dis[p] = (dis[q]-e[i].c)*e[i].r;
if(!vis[p]) {
qu.push(p);
num[p]++;
vis[p]=1;
if(num[p]>m)
return true;
}
}
}
}
if(dis[s]>v)
return true;
return false;
}
int main() {
cnt=0;
memset(dis,0,sizeof(dis));
memset(head,-1,sizeof(head));
scanf("%d%d%d%lf",&m,&n,&s,&v);
double rab,rba,cab,cba;
int a,b;
for(int i=0; i<n; i++) {
scanf("%d%d%lf%lf%lf%lf",&a,&b,&rab,&cab,&rba,&cba);
map(a,b,rab,cab);
map(b,a,rba,cba);
}
if(spfa())
printf("YES\n");
else
printf("NO\n");
return 0;
}
链式前向星-搜狗百科
链式前向星是ssfz神牛Malash创造的(至少Baidu上没有搜到)名词,或许这种数据结构有其他更加正规易懂的名字,但我还是没有搜到。(有一个资料称之为加上next数组前向星,但这个名字实在不好)该数据结构可能是Jason911神牛或其他神牛发明的。
之前不理解,现在理解一些,写一下长长记性 加入我们输入的边是
1 4
2 3
5 6
1 3
2 5
4 6
我们按照
e[cnt].to=b;
e[cnt].next = head[a];
head[a]=cnt++;
方式来存储
可以得到
e[0].to = 4 e[0].next=-1 head[1]=0;
e[1].to=3 e[1].next=-1 head[2]=1;
e[2].to=6 e[2].next=-1 head[5]=2;
e[3].to=3 e[3].next=0 head[1]=3;
e[4].to=5 e[4].next=2 head[4]=4;
e[5].to=6 e[5].next=5 head[4]=5;
从上面就可以看出 e[i].to代表边的终点,head代表边的起始点。 例如以1为起始点的 边有 4 和 3
那么当遍历时 for(int i=head[u] ; i!=-1 ; i=e[i].next )
当 u = 1 时 i= head[u]=3; e[3].to= 3; e[3].next = 0
i = 0; e[0].to=4 ; e[0].next=-1;
这样便找到了所有以1为起始的边.