程序设计思维与实践 Week7 作业 (3/4/数据班)

程序设计思维与实践 Week7 作业 (3/4/数据班)

A - TT 的魔法猫

众所周知,TT 有一只魔法猫。

这一天,TT 正在专心致志地玩《猫和老鼠》游戏,然而比赛还没开始,聪明的魔法猫便告诉了 TT 比赛的最终结果。TT 非常诧异,不仅诧异于他的小猫咪居然会说话,更诧异于这可爱的小不点为何有如此魔力?

魔法猫告诉 TT,它其实拥有一张游戏胜负表,上面有 N 个人以及 M 个胜负关系,每个胜负关系为 A B,表示 A 能胜过 B,且胜负关系具有传递性。即 A 胜过 B,B 胜过 C,则 A 也能胜过 C。

TT 不相信他的小猫咪什么比赛都能预测,因此他想知道有多少对选手的胜负无法预先得知,你能帮帮他吗?

Input

第一行给出数据组数。

每组数据第一行给出 N 和 M(N , M <= 500)。

接下来 M 行,每行给出 A B,表示 A 可以胜过 B。

Output

对于每一组数据,判断有多少场比赛的胜负不能预先得知。注意 (a, b) 与 (b, a) 等价,即每一个二元组只被计算一次。

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

问题分析

构造有向图,利用弗洛伊德算法求路。

dis[i][j]表示两点之间的距离,如果不是通路,记为一个统一的最大整数,遍历途中的每一个点,求最短路,之后统计不互通的点。

优化:常规的三重循环会超时,在第二层循环里,判断i到k是不是有路,如果没有,直接进入下一层循环。

#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 的旅行日记

众所周知,TT 有一只魔法猫。

今天他在 B 站上开启了一次旅行直播,记录他与魔法猫在喵星旅游时的奇遇。 TT 从家里出发,准备乘坐猫猫快线前往喵星机场。猫猫快线分为经济线和商业线两种,它们的速度与价钱都不同。当然啦,商业线要比经济线贵,TT 平常只能坐经济线,但是今天 TT 的魔法猫变出了一张商业线车票,可以坐一站商业线。假设 TT 换乘的时间忽略不计,请你帮 TT 找到一条去喵星机场最快的线路,不然就要误机了!

输入

输入包含多组数据。每组数据第一行为 3 个整数 N, S 和 E (2 ≤ N ≤ 500, 1 ≤ S, E ≤ 100),即猫猫快线中的车站总数,起点和终点(即喵星机场所在站)编号。

下一行包含一个整数 M (1 ≤ M ≤ 1000),即经济线的路段条数。

接下来有 M 行,每行 3 个整数 X, Y, Z (1 ≤ X, Y ≤ N, 1 ≤ Z ≤ 100),表示 TT 可以乘坐经济线在车站 X 和车站 Y 之间往返,其中单程需要 Z 分钟。

下一行为商业线的路段条数 K (1 ≤ K ≤ 1000)。

接下来 K 行是商业线路段的描述,格式同经济线。

所有路段都是双向的,但有可能必须使用商业车票才能到达机场。保证最优解唯一。

输出

对于每组数据,输出3行。第一行按访问顺序给出 TT 经过的各个车站(包括起点和终点),第二行是 TT 换乘商业线的车站编号(如果没有使用商业线车票,输出"Ticket Not Used",不含引号),第三行是 TT 前往喵星机场花费的总时间。

本题不忽略多余的空格和制表符,且每一组答案间要输出一个换行

输入样例
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

问题分析

由于只会乘坐0段或者1段商业线,可遍历商业线路。假定某一次选中IJ段做商业线,那么SI、JE或者SJ、IE段坐经济线...每次记录下来,选出最小值即可。

乘坐经济线的部分,可用两次迪杰斯特拉算法,分别记下起点和终点到其他点的最短距离。

#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 - TT 的美梦

这一晚,TT 做了个美梦!

在梦中,TT 的愿望成真了,他成为了喵星的统领!喵星上有 N 个商业城市,编号 1 ~ N,其中 1 号城市是 TT 所在的城市,即首都。

喵星上共有 M 条有向道路供商业城市相互往来。但是随着喵星商业的日渐繁荣,有些道路变得非常拥挤。正在 TT 为之苦恼之时,他的魔法小猫咪提出了一个解决方案!TT 欣然接受并针对该方案颁布了一项新的政策。

具体政策如下:对每一个商业城市标记一个正整数,表示其繁荣程度,当每一只喵沿道路从一个商业城市走到另一个商业城市时,TT 都会收取它们(目的地繁荣程度 - 出发地繁荣程度)^ 3 的税。

TT 打算测试一下这项政策是否合理,因此他想知道从首都出发,走到其他城市至少要交多少的税,如果总金额小于 3 或者无法到达请悄咪咪地打出 '?'。

Input

第一行输入 T,表明共有 T 组数据。(1 <= T <= 50)

对于每一组数据,第一行输入 N,表示点的个数。(1 <= N <= 200)

第二行输入 N 个整数,表示 1 ~ N 点的权值 a[i]。(0 <= a[i] <= 20)

第三行输入 M,表示有向道路的条数。(0 <= M <= 100000)

接下来 M 行,每行有两个整数 A B,表示存在一条 A 到 B 的有向道路。

接下来给出一个整数 Q,表示询问个数。(0 <= Q <= 100000)

每一次询问给出一个 P,表示求 1 号点到 P 号点的最少税费。

Output

每个询问输出一行,如果不可达或税费小于 3 则输出 '?'。

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:
?
?

问题分析

先查询是否有路,有路的前提下,再计算总金额。

#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;
}

posted @ 2020-04-18 15:39  hyperatom  阅读(128)  评论(0)    收藏  举报