天天看點

kuangbin最短路專題(下)

J - Invitation Cards

In the age of television, not many people attend theater performances. Antique Comedians of Malidinesia are aware of this fact. They want to propagate theater and, most of all, Antique Comedies. They have printed invitation cards with all the necessary information and with the programme. A lot of students were hired to distribute these invitations among the people. Each student volunteer has assigned exactly one bus stop and he or she stays there the whole day and gives invitation to people travelling by bus. A special course was taken where students learned how to influence people and what is the difference between influencing and robbery. 

The transport system is very special: all lines are unidirectional and connect exactly two stops. Buses leave the originating stop with passangers each half an hour. After reaching the destination stop they return empty to the originating stop, where they wait until the next full half an hour, e.g. X:00 or X:30, where 'X' denotes the hour. The fee for transport between two stops is given by special tables and is payable on the spot. The lines are planned in such a way, that each round trip (i.e. a journey starting and finishing at the same stop) passes through a Central Checkpoint Stop (CCS) where each passenger has to pass a thorough check including body scan. 

All the ACM student members leave the CCS each morning. Each volunteer is to move to one predetermined stop to invite passengers. There are as many volunteers as stops. At the end of the day, all students travel back to CCS. You are to write a computer program that helps ACM to minimize the amount of money to pay every day for the transport of their employees. 

Input

The input consists of N cases. The first line of the input contains only positive integer N. Then follow the cases. Each case begins with a line containing exactly two integers P and Q, 1 <= P,Q <= 1000000. P is the number of stops including CCS and Q the number of bus lines. Then there are Q lines, each describing one bus line. Each of the lines contains exactly three numbers - the originating stop, the destination stop and the price. The CCS is designated by number 1. Prices are positive integers the sum of which is smaller than 1000000000. You can also assume it is always possible to get from any stop to any other stop.

Output

For each case, print one line containing the minimum amount of money to be paid each day by ACM for the travel costs of its volunteers.

Sample Input

2
2 2
1 2 13
2 1 33
4 6
1 2 10
2 1 60
1 3 20
3 4 10
2 4 5
4 1 50
           

Sample Output

46
210
           

題意:前面描述冗長可以先跳過,直接看input和output部分,看個大體意思再看一下描述的最後一段,基本就了解題意了。有若幹城市和單向公交路線,現在很多acm志願者在1處,每個志願者去一個城市,求志願者到每個城市再回來的總時間。

思路:首先城市數量最多1000000個,所有不能用鄰接矩陣隻能用鄰接表了。先求出1到所有其他點的總時間,存起來。再将所有單向路逆置,再求1到所有其他點的總時間存起來。兩部分相加就是答案。

注:答案用long long 存。 輸入用scanf。

代碼:

//Full of love and hope for the life

//https://paste.ubuntu.com/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#define BIG  10000
#define inf 0x3f3f3f3f

typedef long long ll;

using namespace std;

struct pre{
    int v,w,next;
}edge[5000010];

struct node{
    int data;
    int v;
};

node g,h;
int a,b,c,ma;
int flag[1000010];
int disv[1000010];
int head[1000010];
int cnt;
ll ans;
int u[1000010],v[1000010],w[1000010];// 友善之後鄰接表的逆置

void add(int u,int v,int w){// 鄰接表的存儲
     edge[cnt].v=v;
     edge[cnt].w=w;
     edge[cnt].next=head[u];
     head[u]=cnt++;
}

bool operator < (const node &a,const node &b){
    return a.data>b.data;
}

void dij(){// 堆優化的dijkstra
    priority_queue<node>q;
    memset(disv,inf,sizeof(disv));// 初始距離為inf
    memset(flag,0,sizeof(flag));
    g.data=0;
    g.v=1;
    q.push(g);
    while(!q.empty()){
        if(flag[q.top().v]==1){
            q.pop();
            continue;
        }
        h=q.top();
        disv[h.v]=h.data;
        flag[h.v]=1;
        for(int i=head[h.v]; i!=-1; i=edge[i].next){
                int j=edge[i].v;
            if(flag[j]==0&&disv[j]>disv[h.v]+edge[i].w){
                disv[j]=disv[h.v]+edge[i].w;
                g.v=j;
                g.data=disv[j];
                q.push(g);
            }
        }
    }
    for(int i=1; i<=a; i++){// 存一下答案
        ans+=disv[i];
    }
}

int main(){
    int T;
    cin >> T;
    while(T--){
    ans=0;
    cin >> a >> b;
    ma=0;
    cnt=0;
    memset(head,-1,sizeof(head)); // 初始化為-1
    for(int i=1; i<=b; i++){
        scanf("%d%d%d",&u[i],&v[i],&w[i]);// 用cin會逾時
        add(u[i],v[i],w[i]);
    }
    dij();
    cnt=0;
    memset(head,-1,sizeof(head));
    for(int i=1; i<=b; i++){
        add(v[i],u[i],w[i]);
    }
    dij();
    cout << ans << endl;
    }
    return 0;
}
           

K - Candies

During the kindergarten days, flymouse was the monitor of his class. Occasionally the head-teacher brought the kids of flymouse’s class a large bag of candies and had flymouse distribute them. All the kids loved candies very much and often compared the numbers of candies they got with others. A kid A could had the idea that though it might be the case that another kid B was better than him in some aspect and therefore had a reason for deserving more candies than he did, he should never get a certain number of candies fewer than B did no matter how many candies he actually got, otherwise he would feel dissatisfied and go to the head-teacher to complain about flymouse’s biased distribution.

snoopy shared class with flymouse at that time. flymouse always compared the number of his candies with that of snoopy’s. He wanted to make the difference between the numbers as large as possible while keeping every kid satisfied. Now he had just got another bag of candies from the head-teacher, what was the largest difference he could make out of it?

Input

The input contains a single test cases. The test cases starts with a line with two integers N and M not exceeding 30 000 and 150 000 respectively. N is the number of kids in the class and the kids were numbered 1 through N. snoopy and flymouse were always numbered 1 and N. Then follow M lines each holding three integers A, B and cin order, meaning that kid A believed that kid B should never get over c candies more than he did.

Output

Output one line with only the largest difference desired. The difference is guaranteed to be finite.

Sample Input

2 2
1 2 5
2 1 4
           

Sample Output

5
           

Hint

32-bit signed integer type is capable of doing all arithmetic.

題意:差分限制。

總共n個點,m對 A B c 表示B-A<=c

要求1和n的內插補點最大

每個限制B-A<=c 就是B<=A+c 加邊A->B 為c的邊。

建圖以後就是求最短路。

思路:非常經典的一道最短路的題了,有30005個點,用鄰接表+堆優化的dijkstra做。

注:之是以用鄰接表是因為不能定義a[30005][30005], 也間接說明了資料量很大,所有讀入資料一定要用scanf,用cin會逾時。

偶然發現了kuangbin本人的部落格,引用了一下題意:https://www.cnblogs.com/kuangbin/p/3141375.html

代碼:

//Full of love and hope for life

#include <iostream>
#include <algorithm>
#include <stdio.h>
#include <string.h>
#include <math.h>
#include <queue>
#include <map>
#define inf 0x3f3f3f
//https://paste.ubuntu.com/

using namespace std;

const int N = 30010;

struct pre{
    int v,w,next;
}edge[150010]; // 這個存邊,設的大一點

struct node{ // 定義優先隊列
    int data,v;
};

int flag[30010];
int disv[30010];
int head[30010];
node g,h;
int a,b;
int cnt;

bool operator < (const node &a,const node &b){ // 定義優先隊列
      return a.data>b.data;
}

void add(int u,int v,int w)
{
    edge[cnt].v=v;
    edge[cnt].w=w;
    edge[cnt].next=head[u];
    head[u]=cnt++;
}

void dij(){ // 求最短路。堆優化的dijkstra
    disv[1]=0;
    priority_queue<node>q;
    g.v=1;
    g.data=0;
    q.push(g);
    while(!q.empty()){
        if(flag[q.top().v]==1){
            q.pop();
            continue;
        }
        h=q.top();
        flag[h.v]=1;
        disv[h.v]=h.data;
        for(int i=head[h.v];i!=-1;i=edge[i].next){// 這一塊for代碼容易出錯,好好了解
                int j=edge[i].v;
            if(flag[j]==0&&disv[j]>disv[h.v]+edge[i].w){
                disv[j]=disv[h.v]+edge[i].w;
                g.v=j;
                g.data=disv[j];
                q.push(g);
            }
        }
    }
    cout << disv[a];
}

int main(){
    //ios::sync_with_stdio(false);//也逾時
    //用cin逾時
    int u,v,w;
    scanf("%d%d",&a,&b);
    memset(flag,0,sizeof(flag));
    memset(disv,inf,sizeof(disv));
    memset(head,-1,sizeof(head));// head初始化為-1
    cnt=0;
    for(int i=1;i<=b;i++){
        scanf("%d%d%d",&u,&v,&w); // 用鄰接表的時候必須用scanf
        add(u,v,w);
    }
    dij();
    return 0;
}
           

L - Subway(待補題)

You have just moved from a quiet Waterloo neighbourhood to a big, noisy city. Instead of getting to ride your bike to school every day, you now get to walk and take the subway. Because you don't want to be late for class, you want to know how long it will take you to get to school. 

You walk at a speed of 10 km/h. The subway travels at 40 km/h. Assume that you are lucky, and whenever you arrive at a subway station, a train is there that you can board immediately. You may get on and off the subway any number of times, and you may switch between different subway lines if you wish. All subway lines go in both directions.

Input

Input consists of the x,y coordinates of your home and your school, followed by specifications of several subway lines. Each subway line consists of the non-negative integer x,y coordinates of each stop on the line, in order. You may assume the subway runs in a straight line between adjacent stops, and the coordinates represent an integral number of metres. Each line has at least two stops. The end of each subway line is followed by the dummy coordinate pair -1,-1. In total there are at most 200 subway stops in the city.

Output

Output is the number of minutes it will take you to get to school, rounded to the nearest minute, taking the fastest route.

Sample Input

0 0 10000 1000
0 200 5000 200 7000 200 -1 -1 
2000 600 5000 600 10000 600 -1 -1
           

Sample Output

21
           

 題目大意:一個人從家要到學校去,途中有許多車站,是以有步行和做地鐵兩種方式,其速度分别是10km/h 和40km/h。輸入的規則是第一行輸入的是x1,y1,x2,y2,分别代表家的坐标和學校的坐标。以後輸入的是車站的坐标,數目不超過200,相鄰的兩個站點可以坐地鐵,其他的需要步行。問到達學校的最短時間是多少?

         因為不知道輸入的資料有多少,是以用while(scanf()!=EOF)。其他的就沒有什麼要注意的了,建圖很重要。

代碼:

//Full of love and hope for the life

//https://paste.ubuntu.com/

#include <cstdio>
#include <iostream>
#include <cstring>
#include <algorithm>
#include <queue>
#include <map>
#include <math.h>
#define BIG  10000
#define inf 0x3f3f3f3f

typedef long long ll;

using namespace std;

#define INF 100000000.0
const int maxn = 450;

struct node{
	double x,y;
}ss[maxn];

double ma[maxn][maxn],dis[maxn];

int vis[maxn];
int cnt;

double get_len(double x1,double y1,double x2,double y2){
	return sqrt((x1-x2)*(x1-x2)+(y1-y2)*(y1-y2));
}

void dijkstra(int st){
	for(int i = 0;i<cnt;i++){
		dis[i] = ma[st][i];
	}
	vis[st] = 1;
	dis[st] = 0.0;
	int pos = st;
	for(int i = 0;i<cnt-1;i++){
		for(int j = 0;j<cnt;j++){
			if(!vis[j] && dis[j] > dis[pos] + ma[pos][j]){
				dis[j] = dis[pos] + ma[pos][j];
			}
		}
		int v;
		double minn = INF;
		for(int j = 0;j<cnt;j++){
			if(!vis[j] && minn>dis[j]){
				v = j;
				minn = dis[j];
			}
		}
		vis[v] = 1;
		pos = v;
	 }
}

int main(){
	memset(vis,0,sizeof(vis));
	memset(ma,0,sizeof(ma));
	double x,y;
	scanf("%lf %lf %lf %lf",&ss[0].x,&ss[0].y,&ss[1].x,&ss[1].y);
	int tra = 2;
	cnt = 2;
	while(scanf("%lf %lf",&x,&y)!=EOF){
		if( x == -1 && y == -1){
			for(int i = tra;i<cnt-1;i++){
				double len = get_len(ss[i].x,ss[i].y,ss[i+1].x,ss[i+1].y)/40000.0;
				ma[i][i+1] = ma[i+1][i] = len;
			}
			tra = cnt;
			continue;
		}
		ss[cnt].x = x;
		ss[cnt++].y = y;
	}
	for(int i = 0;i<cnt;i++){
		for(int j = i+1;j<cnt;j++){
			if(ma[i][j] == 0){
				double len = get_len(ss[i].x,ss[i].y,ss[j].x,ss[j].y)/10000.0;
				ma[i][j] = ma[j][i] = len;
			}
		 }
	}
	dijkstra(1);
	printf("%d",(int)(dis[1] * 60.0 +0.5));
	return 0;
}
//我是真的難啊