最短路径一

收拾心情吧,再次出发吧
写了有些日子的一个最短路径,本想全部写完再写博客,但是题目有点多,先写了九题,放上来吧
https://vjudge.net/contest/260468#problem
kuangbin:
https://cn.vjudge.net/contest/260468
关于bellman-ford算法
https://blog.csdn.net/sms0101/article/details/73088422
b站某英文讲坛:
https://www.bilibili.com/video/av9621366?from=search&seid=5677899327122500540
https://www.bilibili.com/video/av9621366?p=2
https://www.bilibili.com/video/av9621366?p=3(这个我也没看)
不想配图了 好累

A - Til the Cows Come Home
Bessie is out in the field and wants to get back to the barn to get as much sleep as possible before Farmer John wakes her for the morning milking. Bessie needs her beauty sleep, so she wants to get back as quickly as possible.

Farmer John’s field has N (2 <= N <= 1000) landmarks in it, uniquely numbered 1..N. Landmark 1 is the barn; the apple tree grove in which Bessie stands all day is landmark N. Cows travel in the field using T (1 <= T <= 2000) bidirectional cow-trails of various lengths between the landmarks. Bessie is not confident of her navigation ability, so she always stays on a trail from its start to its end once she starts it.

Given the trails between the landmarks, determine the minimum distance Bessie must walk to get back to the barn. It is guaranteed that some such route exists.
Input

  • Line 1: Two integers: T and N

  • Lines 2..T+1: Each line describes a trail as three space-separated integers. The first two integers are the landmarks between which the trail travels. The third integer is the length of the trail, range 1..100.
    Output

  • Line 1: A single integer, the minimum distance that Bessie must travel to get from landmark N to landmark 1.
    Sample Input
    5 5
    1 2 20
    2 3 30
    3 4 20
    4 5 20
    1 5 100
    Sample Output
    90
    Hint
    INPUT DETAILS:

There are five landmarks.

OUTPUT DETAILS:

Bessie can get home by following trails 4, 3, 2, and 1.
dijkstra算法
ac code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#include <iostream>
#define inf 99999
using namespace std;
const int maxn=2005;
struct edge{
int from,to,cost;
};
edge cow[4*maxn];
int d[maxn*2];
int t,n;
void wyh(){
for(int i=1;i<=n;i++){
d[i]=inf;
}
d[1]=0;
for(int i=1;i<=n;i++){
for(int j=1;j<=t;j++){
if(d[cow[j].from]>d[cow[j].to]+cow[j].cost)d[cow[j].from]=d[cow[j].to]+cow[j].cost;
if(d[cow[j].to]>d[cow[j].from]+cow[j].cost)d[cow[j].to]=d[cow[j].from]+cow[j].cost;
}
}
cout<<d[n]<<endl;
}
int main(){
ios::sync_with_stdio(false);
cin>>t>>n;
for(int i=1;i<=t;i++){
int a,b,c;
cin>>a>>b>>c;
cow[i].from=a;
cow[i].to=b;
cow[i].cost=c;
// cow[i+t].from=cow[i].to;
// cow[i+t].to=cow[i].from;
// cow[i+t].cost=cow[i].cost;
}
wyh();
return 0;
}

B - Frogger
Freddy Frog is sitting on a stone in the middle of a lake. Suddenly he notices Fiona Frog who is sitting on another stone. He plans to visit her, but since the water is dirty and full of tourists’ sunscreen, he wants to avoid swimming and instead reach her by jumping.
Unfortunately Fiona’s stone is out of his jump range. Therefore Freddy considers to use other stones as intermediate stops and reach her by a sequence of several small jumps.
To execute a given sequence of jumps, a frog’s jump range obviously must be at least as long as the longest jump occuring in the sequence.
The frog distance (humans also call it minimax distance) between two stones therefore is defined as the minimum necessary jump range over all possible paths between the two stones.

You are given the coordinates of Freddy’s stone, Fiona’s stone and all other stones in the lake. Your job is to compute the frog distance between Freddy’s and Fiona’s stone.
Input
The input will contain one or more test cases. The first line of each test case will contain the number of stones n (2<=n<=200). The next n lines each contain two integers xi,yi (0 <= xi,yi <= 1000) representing the coordinates of stone #i. Stone #1 is Freddy’s stone, stone #2 is Fiona’s stone, the other n-2 stones are unoccupied. There’s a blank line following each test case. Input is terminated by a value of zero (0) for n.
Output
For each test case, print a line saying “Scenario #x” and a line saying “Frog Distance = y” where x is replaced by the test case number (they are numbered from 1) and y is replaced by the appropriate real number, printed to three decimals. Put a blank line after each test case, even after the last one.
Sample Input
2
0 0
3 4

3
17 4
19 4
18 5

0
Sample Output
Scenario #1
Frog Distance = 5.000

Scenario #2
Frog Distance = 1.414
依旧dijstra
ac code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62

#include <iostream>
#include <cmath>
#include <algorithm>
#include <cstring>
#include <iomanip>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
bool vis[202];
double d[202];
double cost[202][202];
int n;
struct cnm{
int x,y;
}pos[502];
void dijstra(){
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++){
d[i]=inf;
}
d[1]=0;
for(int i=0;i<n;i++){
int v=-1;
double sum=inf;
for(int j=1;j<=n;j++){
if(!vis[j]&&d[j]<sum){
sum=d[j];
v=j;
}
}
if(v==-1)break;
vis[v]=true;
for(int j=1;j<=n;j++){
d[j]=min(d[j],max(d[v],cost[v][j]));

}
}
}
int main(){
ios::sync_with_stdio(false);
int cnt=0;
while(cin>>n,n){
cnt++;
for(int i=1;i<=n;i++){
cin>>pos[i].x>>pos[i].y;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
cost[j][i]=cost[i][j]=sqrt((double)(pos[i].x-pos[j].x)*(pos[i].x-pos[j].x)+(pos[i].y-pos[j].y)*(pos[i].y-pos[j].y));
}
}
dijstra();
if(cnt!=1)cout<<endl;
cout<<"Scenario #"<<cnt<<endl<<"Frog Distance = "<<setiosflags(ios::fixed)<<setprecision(3)<<d[2]<<endl;
/*cout<<"cnmbnmsl:"<<endl;
for(int i=1;i<=n;i++){
cout<<d[i]<<endl;
}*/
}
return 0;
}

Heavy Transportation
Background
Hugo Heavy is happy. After the breakdown of the Cargolifter project he can now expand business. But he needs a clever man who tells him whether there really is a way from the place his customer has build his giant steel crane to the place where it is needed on which all streets can carry the weight.
Fortunately he already has a plan of the city with all streets and bridges and all the allowed weights.Unfortunately he has no idea how to find the the maximum weight capacity in order to tell his customer how heavy the crane may become. But you surely know.

Problem
You are given the plan of the city, described by the streets (with weight limits) between the crossings, which are numbered from 1 to n. Your task is to find the maximum weight that can be transported from crossing 1 (Hugo’s place) to crossing n (the customer’s place). You may assume that there is at least one path. All streets can be travelled in both directions.
Input
The first line contains the number of scenarios (city plans). For each city the number n of street crossings (1 <= n <= 1000) and number m of streets are given on the first line. The following m lines contain triples of integers specifying start and end crossing of the street and the maximum allowed weight, which is positive and not larger than 1000000. There will be at most one street between each pair of crossings.
Output
The output for every scenario begins with a line containing “Scenario #i:”, where i is the number of the scenario starting at 1. Then print a single line containing the maximum allowed weight that Hugo can transport to the customer. Terminate the output for the scenario with a blank line.
Sample Input
1
3 3
1 2 3
1 3 4
2 3 5
Sample Output
Scenario #1:
4
还是dijstra:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
#include <iostream>
#include <cstring>
#include <algorithm>
#include <cmath>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=1111;
int cost[maxn][maxn];
bool vis[maxn];
int d[maxn];
int n,m;
int max_cost;
void dijstra(){
memset(vis,false,sizeof(vis));
for(int i=1;i<=n;i++){
d[i]=cost[1][i];
}

for(int i=0;i<n;i++){
int v=-1;
int sum=0;
for(int j=1;j<=n;j++){
if(!vis[j]&&d[j]>sum){
sum=d[j];
v=j;
}
}
if(v==-1)break;
vis[v]=true;
for(int j=1;j<=n;j++){
d[j]=max(d[j],min(d[v],cost[v][j]));
}
}
}
int main(){
ios::sync_with_stdio(false);
int t,ans=1;
cin>>t;
//ans=t;
while(t--){
memset(cost,0,sizeof(cost));
cin>>n>>m;
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
cost[a][b]=cost[b][a]=c;
}
dijstra();
cout<<"Scenario #"<<ans++<<":"<<endl;
cout<<d[n]<<endl;
cout<<endl;
}
return 0;
}

Silver Cow Party

One cow from each of N farms (1 ≤ N ≤ 1000) conveniently numbered 1..N is going to attend the big cow party to be held at farm #X (1 ≤ X ≤ N). A total of M (1 ≤ M ≤ 100,000) unidirectional (one-way roads connects pairs of farms; road i requires Ti (1 ≤ Ti ≤ 100) units of time to traverse.

Each cow must walk to the party and, when the party is over, return to her farm. Each cow is lazy and thus picks an optimal route with the shortest time. A cow’s return route might be different from her original route to the party since roads are one-way.

Of all the cows, what is the longest amount of time a cow must spend walking to the party and back?

Input
Line 1: Three space-separated integers, respectively: N, M, and X
Lines 2.. M+1: Line i+1 describes road i with three space-separated integers: Ai, Bi, and Ti. The described road runs from farm Ai to farm Bi, requiring Ti time units to traverse.
Output
Line 1: One integer: the maximum of time any one cow must walk.
Sample Input
4 8 2
1 2 4
1 3 2
1 4 7
2 1 1
2 3 5
3 1 2
3 4 4
4 2 3
Sample Output
10
Hint
Cow 4 proceeds directly to the party (3 units) and returns via farms 1 and 3 (7 units), for a total of 10 time units.
这题bellman-ford算法:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
#include <iostream>
#include <algorithm>
#include <cstring>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=111111;
struct edge{
int from,to,cost;
};
edge wyh[maxn];
int n,m,x;
int d[maxn];
int short_path(int s,int e){
memset(d,inf,sizeof(d));
d[s]=0;
while(1){
bool cnm=false;
for(int i=1;i<=m;i++){
edge e=wyh[i];
if(d[e.from]!=inf&&d[e.to]>d[e.from]+e.cost){
d[e.to]=d[e.from]+e.cost;
cnm=true;
}
}
if(!cnm)break;
}
return d[e];
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m>>x;
for(int i=1;i<=m;i++){
int a,b,c;
cin>>a>>b>>c;
wyh[i].from=a;
wyh[i].to=b;
wyh[i].cost=c;
}
int path_max=0;
for(int i=1;i<=n;i++){
path_max=max(path_max,short_path(i,x)+short_path(x,i));
}
cout<<path_max<<endl;
return 0;
}

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

bellman-ford
:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
#include <iostream>
#include <cstdio>
#include <cstring>
#define ll long long
#define minmin 1e-9
using namespace std;
struct edge{
int from,to;
double r,c;
};
edge pos[400];
int n,m,x;
double v;
double d[205];
bool bellman_are_you_ok(){
memset(d,0.0,sizeof(d));
d[x]=v;
bool flag;
for(int i=1;i<n;i++){
flag=true;
for(int j=1;j<=m;j++){
double temp=(d[pos[j].from]-pos[j].c)*pos[j].r;
if(d[pos[j].to]<temp){
flag=false;
d[pos[j].to]=temp;
}
}
if(flag)return false;
}
for(int i=1;i<=m;i++){
if(d[pos[i].to]<(d[pos[i].from]-pos[i].c)*pos[i].r){
return true;
}
}
return false;
}
int main(){
scanf("%d%d%d%lf",&n,&m,&x,&v);
int k=1;
int a,b;
double c,d,e,f;
for(int i=1;i<=m;i++){
scanf("%d%d%lf%lf%lf%lf",&a,&b,&c,&d,&e,&f);
pos[k].from=a;
pos[k].to=b;
pos[k].r=c;
pos[k++].c=d;
pos[k].from=b;
pos[k].to=a;
pos[k].r=e;
pos[k++].c=f;
}
m=2*m;
if(bellman_are_you_ok())cout<<"YES"<<endl;
else cout<<"NO"<<endl;
return 0;
}

Wormholes
While exploring his many farms, Farmer John has discovered a number of amazing wormholes. A wormhole is very peculiar because it is a one-way path that delivers you to its destination at a time that is BEFORE you entered the wormhole! Each of FJ’s farms comprises N (1 ≤ N ≤ 500) fields conveniently numbered 1..N, M (1 ≤ M ≤ 2500) paths, and W (1 ≤ W ≤ 200) wormholes.

As FJ is an avid time-traveling fan, he wants to do the following: start at some field, travel through some paths and wormholes, and return to the starting field a time before his initial departure. Perhaps he will be able to meet himself :) .

To help FJ find out whether this is possible or not, he will supply you with complete maps to F (1 ≤ F ≤ 5) of his farms. No paths will take longer than 10,000 seconds to travel and no wormhole can bring FJ back in time by more than 10,000 seconds.

Input
Line 1: A single integer, F. F farm descriptions follow.
Line 1 of each farm: Three space-separated integers respectively: N, M, and W
Lines 2.. M+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: a bidirectional path between S and E that requires T seconds to traverse. Two fields might be connected by more than one path.
Lines M+2.. M+ W+1 of each farm: Three space-separated numbers ( S, E, T) that describe, respectively: A one way path from S to E that also moves the traveler back T seconds.
Output
Lines 1.. F: For each farm, output “YES” if FJ can achieve his goal, otherwise output “NO” (do not include the quotes).
Sample Input
2
3 3 1
1 2 2
1 3 4
2 3 1
3 1 3
3 2 1
1 2 3
2 3 4
3 1 8
Sample Output
NO
YES
Hint
For farm 1, FJ cannot travel back in time.
For farm 2, FJ could travel back in time by the cycle 1->2->3->1, arriving back at his starting location 1 second before he leaves. He could start from anywhere on the cycle to accomplish this

bellman-ford:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
#include <iostream>
#include <cstring>
#include <algorithm>
#define ll long long
#define inf 0x3f3f3f3f
using namespace std;
const int maxn=5005;
struct edge{
int from,to,cost;
}pos[maxn*2];
int t,n,m,w,k;
int d[maxn];
bool bellman_ford(int s){
for(int i=1;i<=n;i++){
d[s]=inf;
}
d[1]=0;
for(int i=0;i<n;i++){
int flag=1;
for(int j=0;j<k;j++){
if(d[pos[j].to]>d[pos[j].from]+pos[j].cost){
flag=0;
d[pos[j].to]=d[pos[j].from]+pos[j].cost;
if(i==n-1)return true;
}
}
if(flag)return false;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
cin>>t;
while(t--){
k=0;
cin>>n>>m>>w;
for(int i=1;i<=m+w;i++){
int a,b,c;
cin>>a>>b>>c;
if(i<=m){
edge wyh,dyx;
wyh.from=a;
wyh.to=b;
wyh.cost=c;
dyx.from=b;
dyx.to=a;
dyx.cost=c;
pos[k++]=wyh;
pos[k++]=dyx;
}
else {
edge wyh;
wyh.from=a;
wyh.to=b;
wyh.cost=-c;
pos[k++]=wyh;
}
}
if(bellman_ford(1))cout<<"YES"<<endl;
else cout<<"NO"<<endl;
}
return 0;
}

MPI Maelstrom
BIT has recently taken delivery of their new supercomputer, a 32 processor Apollo Odyssey distributed shared memory machine with a hierarchical communication subsystem. Valentine McKee’s research advisor, Jack Swigert, has asked her to benchmark the new system.
Since the Apollo is a distributed shared memory machine, memory access and communication times are not uniform,'' Valentine told Swigert.Communication is fast between processors that share the same memory subsystem, but it is slower between processors that are not on the same subsystem. Communication between the Apollo and machines in our lab is slower yet.’’

How is Apollo’s port of the Message Passing Interface (MPI) working out?’’ Swigert asked.

Not so well,'' Valentine replied.To do a broadcast of a message from one processor to all the other n-1 processors, they just do a sequence of n-1 sends. That really serializes things and kills the performance.’’

Is there anything you can do to fix that?’’

Yes,'' smiled Valentine.There is. Once the first processor has sent the message to another, those two can then send messages to two other hosts at the same time. Then there will be four hosts that can send, and so on.’’

Ah, so you can do the broadcast as a binary tree!’’

Not really a binary tree – there are some particular features of our network that we should exploit. The interface cards we have allow each processor to simultaneously send messages to any number of the other processors connected to it. However, the messages don’t necessarily arrive at the destinations at the same time – there is a communication cost involved. In general, we need to take into account the communication costs for each link in our network topologies and plan accordingly to minimize the total time required to do a broadcast.’’
Input
The input will describe the topology of a network connecting n processors. The first line of the input will be n, the number of processors, such that 1 <= n <= 100.

The rest of the input defines an adjacency matrix, A. The adjacency matrix is square and of size n x n. Each of its entries will be either an integer or the character x. The value of A(i,j) indicates the expense of sending a message directly from node i to node j. A value of x for A(i,j) indicates that a message cannot be sent directly from node i to node j.

Note that for a node to send a message to itself does not require network communication, so A(i,i) = 0 for 1 <= i <= n. Also, you may assume that the network is undirected (messages can go in either direction with equal overhead), so that A(i,j) = A(j,i). Thus only the entries on the (strictly) lower triangular portion of A will be supplied.

The input to your program will be the lower triangular section of A. That is, the second line of input will contain one entry, A(2,1). The next line will contain two entries, A(3,1) and A(3,2), and so on.
Output
Your program should output the minimum communication time required to broadcast a message from the first processor to all the other processors.
Sample Input
5
50
30 5
100 20 50
10 x x 10
Sample Output
35

真不明白为什么把ios::sync_with_stdio(false);关掉就AC:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <cmath>
#include <cstring>
#include <cstdio>
#define inf 0x3f3f3f3f
using namespace std;
int pos[101][101];
int n;
int main(){
// ios::sync_with_stdio(false);
while(cin>>n){
char c[4];
memset(pos,inf,sizeof(pos));
for(int i=0;i<n;i++)pos[i][i]=0;
for(int i=1;i<n;i++){
for(int j=0;j<i;j++){
scanf("%s",c);
if(c[0]=='x')pos[j][i]=pos[i][j]=inf;
else {
int len=strlen(c);
int cnm=0;
for(int k=0;k<len;k++)
cnm=cnm*10+(c[k]-'0');
pos[j][i]=pos[i][j]=cnm;

}
}
}
for(int k=0;k<n;k++){
for(int i=0;i<n;i++){
for(int j=0;j<n;j++){
if(pos[i][j]>pos[i][k]+pos[k][j]){
pos[i][j]=pos[i][k]+pos[k][j];
}
}
}
}
int wyh=-inf;
for(int i=0;i<n;i++){
wyh=max(wyh,pos[0][i]);
}
cout<<wyh<<endl;
}
return 0;
}

Cow Contest
N (1 ≤ N ≤ 100) cows, conveniently numbered 1..N, are participating in a programming contest. As we all know, some cows code better than others. Each cow has a certain constant skill rating that is unique among the competitors.

The contest is conducted in several head-to-head rounds, each between two cows. If cow A has a greater skill level than cow B (1 ≤ A ≤ N; 1 ≤ B ≤ N; A ≠ B), then cow A will always beat cow B.

Farmer John is trying to rank the cows by skill level. Given a list the results of M (1 ≤ M ≤ 4,500) two-cow rounds, determine the number of cows whose ranks can be precisely determined from the results. It is guaranteed that the results of the rounds will not be contradictory.

Input

  • Line 1: Two space-separated integers: N and M
  • Lines 2..M+1: Each line contains two space-separated integers that describe the competitors and results (the first integer, A, is the winner) of a single round of competition: A and B

Output

  • Line 1: A single integer representing the number of cows whose ranks can be determined  

Sample Input
5 5
4 3
4 2
3 2
1 2
2 5
Sample Output
2
这题数据小,直接Floyd了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include <iostream>
#include <cstring>
#define inf 0x3f3f3f3f
using namespace std;
int fight[105][105];
int n,m;
void floyd(){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(fight[i][k]==fight[k][j]&&(fight[i][k]==1||fight[i][k]==-1)){
fight[i][j]=fight[i][k];
}
}
}
}
}
int main(){
ios::sync_with_stdio(false);
cin>>n>>m;
memset(fight,inf,sizeof(fight));
for(int i=1;i<=m;i++){
int a,b;
cin>>a>>b;
fight[a][b]=1;
fight[b][a]=-1;
}
floyd();
int ans=0;
for(int i=1;i<=n;i++){
int sum=0;
for(int j=1;j<=n;j++){
if(fight[i][j]!=inf){
sum++;
}
}
if(sum==n-1)ans++;
}
cout<<ans<<endl;
return 0;
}

Arbitrage
Arbitrage is the use of discrepancies in currency exchange rates to transform one unit of a currency into more than one unit of the same currency. For example, suppose that 1 US Dollar buys 0.5 British pound, 1 British pound buys 10.0 French francs, and 1 French franc buys 0.21 US dollar. Then, by converting currencies, a clever trader can start with 1 US dollar and buy 0.5 10.0 0.21 = 1.05 US dollars, making a profit of 5 percent.

Your job is to write a program that takes a list of currency exchange rates as input and then determines whether arbitrage is possible or not.
Input
The input will contain one or more test cases. Om the first line of each test case there is an integer n (1<=n<=30), representing the number of different currencies. The next n lines each contain the name of one currency. Within a name no spaces will appear. The next line contains one integer m, representing the length of the table to follow. The last m lines each contain the name ci of a source currency, a real number rij which represents the exchange rate from ci to cj and a name cj of the destination currency. Exchanges which do not appear in the table are impossible.
Test cases are separated from each other by a blank line. Input is terminated by a value of zero (0) for n.
Output
For each test case, print one line telling whether arbitrage is possible or not in the format “Case case: Yes” respectively “Case case: No”.
Sample Input
3
USDollar
BritishPound
FrenchFranc
3
USDollar 0.5 BritishPound
BritishPound 10.0 FrenchFranc
FrenchFranc 0.21 USDollar

3
USDollar
BritishPound
FrenchFranc
6
USDollar 0.5 BritishPound
USDollar 4.9 FrenchFranc
BritishPound 10.0 FrenchFranc
BritishPound 1.99 USDollar
FrenchFranc 0.09 BritishPound
FrenchFranc 0.19 USDollar

0
Sample Output
Case 1: Yes
Case 2: No
还是Floyd,有点锈的就是这个输入不大好调,另外就是map了

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
#define ll long long
using namespace std;
const int maxn=31;
map<string,int>name;
int n, m;
double rate[maxn][maxn];
bool floyod(){
for(int k=1;k<=n;k++){
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
if(rate[i][j]<rate[i][k]*rate[k][j]){
rate[i][j]=rate[i][k]*rate[k][j];
}
}
}
}
for(int i=1;i<=n;i++){
if(rate[i][i]>1)return true;
}
return false;
}
int main(){
ios::sync_with_stdio(false);
int ans=0;
while(cin>>n,n){
ans++;
for(int i=1;i<=n;i++){
string nn;
cin>>nn;
name[nn]=i;
}
for(int i=1;i<=n;i++){
for(int j=1;j<=n;j++){
rate[i][j]=(i==j)?1:0;
}
}
cin>>m;
while(m--){
string a,b;
double r;
cin>>a>>r>>b;
rate[name[a]][name[b]]=r;
}
cout<<"Case "<<ans<<": ";
if(floyod())cout<<"Yes"<<endl;
else cout<<"No"<<endl;
}
return 0;
}

就写了这九题,后面的如果还写的话再贴上来,其实不大想写,感觉都是板子,就是理解问题和小纠错的毛病
最近心情实在不行,不知道下次写博客什么时候了,最近还疯狂考试…艹

coswindy

-------------本文结束感谢您的阅读-------------