【 题集 】 寒假计划——最短路

    跟随kai巨巨,开始了寒假的刷题计划,简直刷新了我对刷题的看法!

    每个专题,题目很多,我只能以自己的水平,把能写出来的 记一记、、、

    B  Wormholes  POJ 3259

Description

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 toF (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.

    题意:输入一个数F,接下来有F组数据, 输入三个数n, m, w。n 代表点的个数,m 代表双向路径的对数,且权值为正,w 代表单向路径的对数,权值为负。最后要求的就是,判断是否存在负环,存在则输出YES, 不存在就输出NO

    可以用bellman - ford 来做,用的kuangbin 的模版 ,模版有错- -# 搞了半天、、

#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <string.h>
#include <math.h>
#include <vector>
using namespace std;

const int INF = 0x3f3f3f3f;
const int MAXN = 5500;
int dist[MAXN];
int n, m;

struct Edge
{
    int u, v;
    int cost;
    Edge(int _u = 0, int _v = 0, int _cost = 0): u(_u), v(_v), cost(_cost){}
};

vector<Edge> E;

bool bellman_ford(int start)
{
    for(int i = 1; i <= n; i ++)
        dist[i] = INF;
    dist[start] = 0;
    for(int i = 1; i < n; i ++)
    {
        bool flag = false;
        for(int j = 0; j < E.size(); j ++)
        {
            int u = E[j].u;
            int v = E[j].v;
            int cost = E[j].cost;
            if(dist[v] > dist[u] + cost)
            {
                dist[v] = dist[u] + cost;
                flag = true;
            }
        }
        if(!flag)
            break;
    }
    for(int j = 0; j < E.size(); j ++)
    {
        if(dist[E[j].v] > dist[E[j].u] + E[j].cost)
            return true;
    }
    return false;
}

int main()
{
    int T;
    int w, s, e, t;
    while(~scanf("%d",&T))
    {
        while(T --)
        {
            while(!E.empty())
            {
                E.pop_back();
            }
            scanf("%d%d%d",&n,&m,&w);
            for(int i = 1; i <= m ;i ++)
            {
                scanf("%d%d%d",&s, &e, &t);
                E.push_back((Edge){s, e, t});
                E.push_back((Edge){e, s, t});
            }

            for(int j = 1; j <= w; j ++)
            {
                scanf("%d%d%d",&s,&e,&t);
                E.push_back((Edge){s, e, -t});
            }

           /* for(int i = 0; i < E.size(); i ++)
            {
                printf("%d %d %d\n", E[i].u, E[i].v, E[i].cost);
            }*/

            if(bellman_ford(1))
            {
                printf("YES\n");
            }
            else
                printf("NO\n");
        }
    }
}


F  POJ 2240

Description

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 的 一个变种


#include <stdio.h>
#include <iostream>
#include <algorithm>
#include <map>
#include <string.h>
using namespace std;

#define N 110

map <string, int> mp;
double tt[N][N];
int n, m;

bool floyd()
{
    for( int k = 1; k <= n; k ++)
        for( int i = 1; i <= n; i ++)
            for( int j = 1; j <= n; j ++)
                if( tt[i][j] < tt[i][k] * tt[k][j])
                    tt[i][j] = tt[i][k] * tt[k][j];
    for( int i = 1; i <= n; i ++)
        if( tt[i][i] > 1)
            return true;
    return false;
}

int main()
{
    double a;
    char t[110];
    char t1[110];
    char t2[110];
    int icase = 1;
    while(~scanf("%d",&n),n)
    {
        memset(tt, 0, sizeof(tt));
        for(int i = 1; i <= n; i ++)
        {
            scanf("%s",t);
            mp[t] = i;
        }
        scanf("%d",&m);
        for(int i = 1; i <= m; i ++)
        {
            scanf("%s%lf%s", t1, &a, t2);
            tt[ mp[t1] ][ mp[t2] ] = a;
        }
        printf("Case %d: ", icase ++);
        if(floyd())
            printf("Yes\n");
        else
            printf("No\n");
    }
}

E  POJ 1125

Description

Stockbrokers are known to overreact to rumours. You have been contracted to develop a method of spreading disinformation amongst the stockbrokers to give your employer the tactical edge in the stock market. For maximum effect, you have to spread the rumours in the fastest possible way.

Unfortunately for you, stockbrokers only trust information coming from their "Trusted sources" This means you have to take into account the structure of their contacts when starting a rumour. It takes a certain amount of time for a specific stockbroker to pass the rumour on to each of his colleagues. Your task will be to write a program that tells you which stockbroker to choose as your starting point for the rumour, as well as the time it will take for the rumour to spread throughout the stockbroker community. This duration is measured as the time needed for the last person to receive the information.

Input

Your program will input data for different sets of stockbrokers. Each set starts with a line with the number of stockbrokers. Following this is a line for each stockbroker which contains the number of people who they have contact with, who these people are, and the time taken for them to pass the message to each person. The format of each stockbroker line is as follows: The line starts with the number of contacts (n), followed by n pairs of integers, one pair for each contact. Each pair lists first a number referring to the contact (e.g. a '1' means person number one in the set), followed by the time in minutes taken to pass a message to that person. There are no special punctuation symbols or spacing rules.

Each person is numbered 1 through to the number of stockbrokers. The time taken to pass the message on will be between 1 and 10 minutes (inclusive), and the number of contacts will range between 0 and one less than the number of stockbrokers. The number of stockbrokers will range from 1 to 100. The input is terminated by a set of stockbrokers containing 0 (zero) people.

Output

For each set of data, your program must output a single line containing the person who results in the fastest message transmission, and how long before the last person will receive any given message after you give it to this person, measured in integer minutes.
It is possible that your program will receive a network of connections that excludes some persons, i.e. some people may be unreachable. If your program detects such a broken network, simply output the message "disjoint". Note that the time taken to pass the message from person A to person B is not necessarily the same as the time taken to pass it from B to A, if such transmission is possible at all.

Sample Input

3
2 2 4 3 5
2 1 2 3 6
2 1 2 2 2
5
3 4 4 2 8 5 3
1 5 8
4 1 6 4 10 2 7 5 2
0
2 2 5 1 5
0

Sample Output

3 2
3 10


    感觉怪怪的,有点问题、、

   

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std;

const int INF = 0xffffff;

int map[150][150];
int dis[150];
int vis[150];
int n;

int dij(int sour)
{
	int i, j, k;
	int t;
	for(i = 1; i <= n; i ++)
	{
		vis[i] = 0;
		dis[i] = map[sour][i];
	}
	vis[sour] = 1;
	int min;
	for(i = 1; i < n; i ++)
	{
		min = INF;
		t = 0;
		for(j = 1; j <= n; j ++)
		{
			if(!vis[j] && min > dis[j])
			{
				t = j;
				min = dis[j];
			}
		}
		vis[t] = 1;
		for(j = 1; j <= n; j ++)
		{
			int newdis = min + map[t][j];
			if(!vis[j] && dis[j] > newdis)
			{
			    dis[j] = newdis;
			}
		}
	}
    /*for(int i = 1; i <= n; i ++)
    {
        if(vis[i] == 0)
            return -1;
    }*/
    return min;
}

void init()
{
    for(int i = 0 ; i <= n; i ++)
    {
        for(int j = 0; j <= n; j ++)
        {
            map[i][j] = INF;
        }
    }
}

int main()
{
	int a, b;
	while(~scanf("%d",&n), n)
        {
        init();
        for(int j = 1; j <= n; j ++)
        {
            scanf("%d",&a);
            for(int i = 1; i <= a; i ++)
            {
                scanf("%d",&b);
                scanf("%d",&map[j][b]);
            }

        }
        bool flag = false;
        int minn = dij(1);
        int ans = 1;
        for(int i = 2; i <= n; i ++)
        {
            int tmp_1 = dij(i);
            if(tmp_1 == -1)
            {
                printf("disjoint\n");
                break;
                flag = true;
            }
            if(tmp_1 < minn)
            {
                minn = tmp_1;
                ans = i;
            }
        }
        if(flag)
            continue;
        printf("%d %d\n",ans, minn);
    }
    return 0;
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值