Programming Thinking and Practice Week7 Homework (3/4 / Data Class)

Programming Thinking and Practice Week7 Homework (3/4 / Data Class)

A-TT's magic cat

As we all know, TT has a magic cat.

On this day, TT was devoted to playing the "Cat and Mouse" game, but before the game started, the smart magic cat told the final result of the TT game. TT is very surprised. Not only is his kitten actually talking, but also why is this cute little girl so magical?

Magic Cat tells TT that it actually has a game win-loss table with N individuals and M win-loss relationships. Each win-loss relationship is AB, indicating that A can win B, and the win-loss relationship is transitive. That is, A beats B, and B beats C, so A can beat C.

TT does n’t believe that his kitten can predict any game, so he wants to know how many players ’outcomes cannot be known in advance. Can you help him?

The

first line of Input gives the number of data groups.

The first line of each set of data gives N and M (N, M <= 500).

In the next M lines, each line is given AB, indicating that A can beat B.

Output

For each set of data, it is determined how much the outcome of the game can not be known in advance. Note that (a, b) is equivalent to (b, a), that is, each binary is calculated only once.

Sample Input
3
3 3
1 2
1 3
2 3
3 2
1 2
2 3
4 2
1 2
3 4
Sample Output
0
0
4

problem analysis

Construct a directed graph and use Freud algorithm to find the way.

dis[i][j]Represents the distance between two points. If it is not a path, it is recorded as a uniform maximum integer, traversing each point on the way to find the shortest path, and then counting the points that are not interoperable.

Optimization: The conventional triple loop will time out. In the second layer loop, judge whether i to k has a way. If not, go directly to the next layer loop.

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
const int INF = 0x7fffffff;
const int MAXV = 505;
int n,m;
int dis[MAXV][MAXV];
void Floyd(){
	for(int k=1;k<=n;++k){
		for(int i=1;i<=n;++i){
			if(dis[i][k]==INF){
				continue;
			}
			for(int j=1;j<=n;++j){
				if(dis[i][k]!=INF && dis[k][j]!=INF && dis[i][k]+dis[k][j]<dis[i][j]){
					dis[i][j]=dis[i][k]+dis[k][j];
				}
			}
		}
	}
}
int main()
{
	int u,v,w;
	int loop;
	scanf("%d",&loop);
	while(loop--){
		scanf("%d%d",&n,&m);
		fill(dis[0],dis[0]+MAXV*MAXV,INF);
		for(int i=1;i<=n;++i){
			dis[i][i]=0;
		}
		for(int i=1;i<=m;++i){
			scanf("%d%d",&u,&v);
			dis[u][v]=1;
		}
		Floyd();
		int sum=0;
		for(int i=1;i<=n;++i){
			for(int j=1;j<=i;++j){
				if(dis[i][j]==INF&&dis[i][j]==dis[j][i]){
					sum++;
				}
			}
		}
		printf("%d\n",sum);
	}
	return 0;
}

B-TT's travel diary

As we all know, TT has a magic cat.

Today he opened a live broadcast of the trip on Station B, recording his adventures with the magic cat while traveling on the cat. TT sets off from home and prepares to take the Cat Express to Meow Star Airport. The cat cat express line is divided into economic line and commercial line, their speed and price are different. Of course, the commercial line is more expensive than the economic line. TT can usually only take the economic line, but today the magic cat of TT has changed to a commercial line ticket and can take a commercial line. Assuming that the time for TT transfer is negligible, please help TT find the fastest route to Miaoxing Airport, otherwise you will miss the plane!

Input The

input contains multiple sets of data. The first line of each set of data is 3 integers N, S and E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ 100), which is the total number of stations, starting point and end point (that is, the station where the Meowing Airport is located) )Numbering.

The next line contains an integer M (1 ≤ M ≤ 1000), which is the number of sections of the economic line.

Next there are M lines, each line has 3 integers X, Y, Z (1 ≤ X, Y ≤ N, 1 ≤ Z ≤ 100), indicating that TT can take the economic line to and from station X and station Y, of which one way It takes Z minutes.

The number of road segments in the next line of the commercial line is K (1 ≤ K ≤ 1000).

The next line K is a description of the commercial line segment, the format is the same as the economic line.

All sections are bidirectional, but it may be necessary to use a commercial ticket to reach the airport. Ensure that the optimal solution is unique.

Output

For each set of data, output 3 lines. The first line gives the stations (including the start point and the end point) that TT passes through in the order of access. The second line is the station number of the TT transfer to the commercial line (if the commercial line ticket is not used, output "Ticket Not Used" without quotation marks ), The third line is the total time spent by TT to the Meow Star Airport.

This question does not ignore the extra spaces and tabs, and a newline should be output between each set of answers

输入样例
4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3
输出样例
1 2 4
2
5

problem analysis

Since it will only take 0 or 1 section of the commercial line, it can traverse the commercial line. Suppose I select the IJ segment as a commercial line at a certain time, then the SI, JE or SJ, IE segments take the economic line ... each time you record it, you can select the minimum value.

For the part taking the economic line, the Dijkstra algorithm can be used twice to record the shortest distances from the start and end points to other points, respectively.

#include <bits/stdc++.h>

using namespace std;
const int size=5e3+10;
const int N=500+5;
const int inf=0x3ffffff;
struct Edge{
	int to,next,w;
}e[size]; 
Edge other[size];
int head[N],tot,n,vis[N],spre[N],sdis[N],tpre[N],tdis[N];
void add(int x,int y,int w)
{
	e[++tot].to=y,e[tot].next=head[x];
	e[tot].w=w;head[x]=tot;
}
priority_queue<pair<int,int> >q;
void dijkstra(int s,int*dis,int*pre)
{
	while(!q.empty())q.pop();
	memset(vis,0,sizeof(vis));
	for(int i=1;i<=n;i++)
		dis[i]=inf,pre[i]=-1;
	dis[s]=0;pre[s]=s;
	q.push(make_pair(0,s));
	while(!q.empty())
	{
		int x=q.top().second;
		q.pop();
		if(vis[x])continue;
		vis[x]=1;
		for(int i=head[x];i!=-1;i=e[i].next)
		{
			int y=e[i].to,w=e[i].w;
			if(dis[y]>dis[x]+w)
			{
				dis[y]=dis[x]+w;
				pre[y]=x;
				q.push(make_pair(-dis[y],y));
			}
		}
	}
}
void print(int to,int*pre)
{
	if(pre[to]!=to)
	{
		print(pre[to],pre);
		printf(" %d",to);		
	}
	else
	{
		printf("%d",to);
	}
}
int main() {
	int s,e;
	bool enter=false;
	while(~scanf("%d%d%d",&n,&s,&e))
	{
		if(enter)
			printf("\n");
		enter=true;
		fill(head,head+N,-1);
		tot=-1;
		int m;
		scanf("%d",&m);
		while(m--)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			add(x,y,z);
			add(y,x,z);
		}
		int k;
		scanf("%d",&k);
		for(int i=0;i<k;i++)
		{
			int x,y,z;
			scanf("%d%d%d",&x,&y,&z);
			other[i].next=x,other[i].to=y,other[i].w=z;
		}
		dijkstra(s,sdis,spre);
		dijkstra(e,tdis,tpre);
		int point=-1,distance=sdis[e];
		bool sort=true;
		for(int i=0;i<k;i++)
		{
			if(sdis[other[i].next]+tdis[other[i].to]+other[i].w<distance)
			{
				distance=sdis[other[i].next]+tdis[other[i].to]+other[i].w;
				point=i;sort=true;
			}
			if(sdis[other[i].to]+tdis[other[i].next]+other[i].w<distance)
			{
				distance=sdis[other[i].to]+tdis[other[i].next]+other[i].w;
				point=i;sort=false;
			}
		}
		if(point==-1)
		{
			print(e,spre);
		}
		else
		{
			if(sort)
			{
				print(other[point].next,spre);
				int temp=other[point].to;
				while(tpre[temp]!=temp)
				{
					printf(" %d",temp);
					temp=tpre[temp];
				}
				printf(" %d",temp);
			}
			else
			{
				print(other[point].to,spre);
				int temp=other[point].next;
				while(tpre[temp]!=temp)
				{
					printf(" %d",temp);
					temp=tpre[temp];
				}
				printf(" %d",temp);
			}
		}
		printf("\n");
		if(point==-1)
			printf("Ticket Not Used\n");
		else
		{
			if(sort)
				printf("%d\n",other[point].next);
			else
				printf("%d\n",other[point].to);
		}
		printf("%d\n",distance);
	}
	return 0;
}

C-the dream of TT

This night, TT had a sweet dream!

In his dream, TT's wish came true, and he became the leader of Meow Star! There are N commercial cities on the Meow Star, numbered 1 to N, of which City 1 is the city where TT is located, that is, the capital.

There are M directional roads on Meow Star for commercial cities to communicate with each other. However, with the increasing prosperity of Meowstar's business, some roads have become very crowded. While TT was distressed, his magic kitty came up with a solution! TT is pleased to accept and promulgate a new policy for this program.

The specific policy is as follows: mark a positive integer for each commercial city to indicate its prosperity. When each cat walks from one commercial city to another commercial city along the road, TT will charge them (destination prosperity-departure point Prosperity) ^ 3 tax.

TT intends to test whether this policy is reasonable, so he wants to know how much tax must be paid to travel from the capital to other cities. If the total amount is less than 3 or cannot be reached, please quietly type '?'.

Input

T in the first line, indicating that there are T sets of data. (1 <= T <= 50)

For each set of data, enter N in the first row to indicate the number of points. (1 <= N <= 200)

Enter N integers in the second line, which represents the weight a [i] from 1 to N points. (0 <= a [i] <= 20)

Enter M in the third line to indicate the number of directed roads. (0 <= M <= 100000)

Next M lines, each line has two integers AB, indicating that there is a directional road from A to B.

Next, an integer Q is given, indicating the number of inquiries. (0 <= Q <= 100000)

Each inquiry gives a P, which means to find the minimum tax from point 1 to point P.

Output

Each query outputs one line. If it is unreachable or if the tax is less than 3, output '?'

Sample Input
2
5
6 7 8 9 10
6
1 2
2 3
3 4
1 5
5 4
4 5
2
4
5
10
1 2 4 4 5 6 7 8 9 10
10
1 2
2 3
3 1
1 4
4 5
5 6
6 7
7 8
8 9
9 10
2
3 10
Sample Output
Case 1:
3
4
Case 2:
?
?

problem analysis

Check if there is a road first, and then calculate the total amount if there is a road.

#include<bits/stdc++.h>
using namespace std;
const int inf = 0x3ffffff;
int N,M,T,Q,P;
int dis[205];
int a[100005];  
int cnt[205];
int vis[205];
bool dvis[205];

struct edge{
	int u_,v_,w_;
};
queue< int > q;
vector< edge > G[205];
void addE(int u,int v,int w)
{
	edge e_ ; e_.u_ = u,e_.v_=v,e_.w_=w;
	G[u].push_back(e_);
}

void ini()
{
	for(int i=0;i<=N;i++)
	{
		dis[i] = inf;
		vis[i] = 0;
		cnt[i] = 0;
		dvis[i] = 0;
	}
	while(!q.empty()) q.pop();
	for(int i=0;i<205;i++) G[i].clear();
}

void dfs(int y)
{
	dvis[y] = 1;
	for(int i=0;i<G[y].size();i++)
	{
		y = G[y][i].v_;
		if(dvis[y] == 1)  continue;
		dfs(y);
	}
}

void spfa(int s)
{
	dis[s] = 0;
	vis[s] = 1;
	q.push(s);
	while(!q.empty())
	{
		int x = q.front(); q.pop();
		vis[x] = 0;
		for(int i=0;i<G[x].size();i++)
		{
			int y = G[x][i].v_ , w = G[x][i].w_;
					
			if( dis[y] > dis[x] + w )
			{
				dis[y] =w + dis[x];
				cnt[y] = cnt[x]+1;
				if( cnt[y] >= N)
				{
					dfs(y);
				} 
				if(vis[y]==1 || dvis[y]==1)  continue;		
				
				vis[y] = 1;
				q.push(y);
			}
		}
	}
}
int main()
{
	ios::sync_with_stdio(0);
	cin>>T;
	int A,B,cost;
	for(int Ti=1;Ti<=T;Ti++)
	{
		cin>>N;
		ini();
		
		for(int Ai=1;Ai<=N;Ai++)
		{
			cin>>a[Ai];
		}
		cin>>M;
		for(int Gi=0;Gi<M;Gi++)
		{
			cin>>A>>B;
			cost =pow( a[B]-a[A] , 3 ) ;
			addE(A,B,cost);
		}
		spfa(1);
		
		cin>>Q;
		cout<<"Case "<<Ti<<":"<<endl;
		for(int Qi=0;Qi<Q;Qi++)
		{
			cin>>P;
			//输出最少税费 			
				if(dvis[P] || dis[P] == inf || dis[P]<3) 
				{cout<<"?"<<endl;
				continue;}
				
				cout<<dis[P]<<endl;			
		}
	}
	return 0;
}

Guess you like

Origin www.cnblogs.com/master-cn/p/12726207.html