题目一 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) 等价,即每一个二元组只被计算一次。
example
input 1
3
3 3
1 2
1 3
2 3
3 2
1 2
2 3
4 2
1 2
3 4
output 1
0
0
4
做法与思路
很简单的一道题,由于数据量很小仅为5e2,所以可以直接使用弗洛伊德算法进行求解,总共有n*(n-1)2种胜负关系,每出现一种减一即可。
代码
#include <iostream>
using namespace std;
int n,m;
bool visit[510][510];
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
int main(int argc, char** argv) {
int zu;
cin>>zu;
for(int k=0;k<zu;k++)
{
cin>>n>>m;
int total=n*(n-1)/2;
int sum=0;
for(int i=0;i<=n;i++)
for(int j=0;j<=n;j++)
visit[i][j]=0;
for(int i=0;i<m;i++)
{
int a,b;
cin>>a>>b;
visit[a][b]=1;
sum++;
}
for(int i=1;i<=n;i++)
{
for(int j=1;j<=n;j++)
{
if(!visit[j][i])
continue;
for(int t=1;t<=n;t++)
{
if((visit[i][t])&&(visit[j][t]==0))
{
visit[j][t]=1;
sum++;
}
}
}
}
cout<<total-sum<<endl;
}
return 0;
}
题目二 tt的旅行日记
题目描述
众所周知,TT 有一只魔法猫。
今天他在 B 站上开启了一次旅行直播,记录他与魔法猫在喵星旅游时的奇遇。 TT 从家里出发,准备乘坐猫猫快线前往喵星机场。猫猫快线分为经济线和商业线两种,它们的速度与价钱都不同。当然啦,商业线要比经济线贵,TT 平常只能坐经济线,但是今天 TT 的魔法猫变出了一张商业线车票,可以坐一站商业线。假设 TT 换乘的时间忽略不计,请你帮 TT 找到一条去喵星机场最快的线路,不然就要误机了!
input
输入包含多组数据。每组数据第一行为 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 行是商业线路段的描述,格式同经济线。
所有路段都是双向的,但有可能必须使用商业车票才能到达机场。保证最优解唯一。
output
对于每组数据,输出3行。第一行按访问顺序给出 TT 经过的各个车站(包括起点和终点),第二行是 TT 换乘商业线的车站编号(如果没有使用商业线车票,输出"Ticket Not Used",不含引号),第三行是 TT 前往喵星机场花费的总时间。
本题不忽略多余的空格和制表符,且每一组答案间要输出一个换行
example
input 1
4 1 4
4
1 2 2
1 3 3
2 4 4
3 4 5
1
2 4 3
output 1
1 2 4
2
5
做法与思路
求最短路问题的变形,一道思路本身不是很困难的题目,但由于一些很隐蔽(至少对我来说很隐蔽)的坑导致了我交了32次才过…令人绝望。
首先确定一下大体的思路,由于只能走一次商业线,所以思考起来不是很困难,设商业线为uv,那么我们在只有普通线的图中使用dijkstra算法统计起点到u的最短距离,再统计终点到v的最短距离,加上uv本身的距离后,求出最小距离即可,同时要记录到达每个点的前置点以便输出路径。
到要注意几点:
1.uv是双向的,所以还要求取起点到v,终点到u;
2.可能没有经过商业线,比较时不要忘记直达的情况;
3.使用回溯路径进行输出时要注意应该从终点回溯到v,从u回溯到起点,而不能直接从终点回溯到起点。既然距离是分开计算求和,那么路径也是如此。
代码
#include <iostream>
#include <cstring>
#include <queue>
using namespace std;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct edge{
int u,v,w,next;
edge(){ u=0; v=0; w=0;next=0;}
edge(int a,int b,int c,int d)
{
u=a;v=b;w=c;next=d;
}
};
struct juli{
int distance,n;
juli(){distance=0,n=0;};
juli(int a,int b){distance=a;n=b;}
bool operator <(const juli& b) const
{
return distance>b.distance;
}
};
bool cishu=0;
int total=0;
edge e[2010];
int head[510];
int pre1[510];
int pre2[510];
bool visit[510];
int dis1[510];
int dis2[510];
int np,s,f,ne,ne2;
void dj(int s,int dis[],int pre[])
{
for(int i=0;i<np+2;i++)
{
dis[i]=100000000;
pre[i]=0;
visit[i]=0;
}
dis[s]=0;
pre[s]=-1;
priority_queue<juli> t;
t.push(juli(0,s));
while(t.size())
{
int tem=t.top().n;
t.pop();
if(visit[tem]) continue;
visit[tem]=1;
for(int i=head[tem];i!=-1;i=e[i].next)
{
int v=e[i].v,w=e[i].w;
if(dis[v]>dis[tem]+w)
{
dis[v]=dis[tem]+w;
pre[v]=tem;
t.push(juli(dis[v],v));
}
}
}
}
void add(int a,int b,int c)
{
e[total]=edge(a,b,c,head[a]);
head[a]=total;
total++;
}
void output1(int s,int t)
{
if(s==t)
{
printf("%d",s);
return;
}
output1(pre1[s],t);
printf(" %d",s);
}
void output2(int s,int t)
{
while(s!=t)
{
printf(" %d",s);
s=pre2[s];
}
printf(" %d",s);
}
int main(int argc, char** argv) {
while(scanf("%d %d %d",&np,&s,&f)!=EOF)
{
scanf("%d",&ne);
total=0;
memset(head,-1,sizeof(head));
for(int i=0;i<ne;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
add(a,b,c);
add(b,a,c);
}
dj(s,dis1,pre1);
dj(f,dis2,pre2);
int diss=dis1[f];
bool judge=0;
int zhongzhuan=0;
int zhongzhuan2=0;
scanf("%d",&ne2);
for(int i=0;i<ne2;i++)
{
int a,b,c;
scanf("%d %d %d",&a,&b,&c);
int d1=dis1[a]+dis2[b]+c;
int d2=dis1[b]+dis2[a]+c;
if(diss>d1)
{
diss=d1;
judge=1;zhongzhuan=a;zhongzhuan2=b;
}
if(diss>d2)
{
diss=d2;
judge=1;zhongzhuan=b;zhongzhuan2=a;
}
}
if(cishu)
{
printf("\n");
}
cishu=1;
if(judge)
{
output1(zhongzhuan,s);
output2(zhongzhuan2,f);
printf("\n%d\n",zhongzhuan);
}
else
{
output1(f,s);
printf("\n");
printf("Ticket Not Used\n");
}
printf("%d\n",diss);
}
return 0;
}
题目三 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 则输出 ‘?’。
example
input 1
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
output 1
Case 1:
3
4
Case 2:
?
?
做法与思路
求最短路(含负边权)问题,应该使用spfa算法。
没什么特别需要处理的地方,套上spfa的模板,然后特别处理一下判断出现负环时的情况。
判断出现负环时,应选取负环中一点进行搜索,将所有它可以到达的点都进行标记,因为负环上的点可以视作负无穷,那么负环可到达的点距离自然也是负无穷。
代码
#include <iostream>
#include <cmath>
#include <algorithm>
#include <queue>
using namespace std;
const int inf=1000000000;
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
struct edge{
int u,v,w;
edge(int a=0,int b=0,int c=0)
{
u=a;v=b;w=c;
}
};
int dis[210];
int point[210];
bool inq[210];
bool ino[210];
int cnt[210];
int main(int argc, char** argv) {
int cishu;
cin>>cishu;
for(int ci=1;ci<=cishu;ci++)
{
int np;//点数
cin>>np;
for(int i=1;i<=np;i++)
{
cin>>point[i];
dis[i]=inf;
inq[i]=0;
cnt[i]=0;
ino[i]=0;
}
inq[0]=0;
ino[0]=0;
cnt[0]=0;
dis[0]=inf;
vector<edge> p[np+1];//每个点相邻的边
int ne;//边数
cin>>ne;
for(int i=1;i<=ne;i++)
{
int a,b;
cin>>a>>b;
int c=pow(point[b]-point[a],3);
//cout<<"b "<<point[b]<<" a "<<point[a]<<" c "<<c<<endl;
p[a].push_back(edge(a,b,c));
}
queue<int> t;
t.push(1);
dis[1]=0;
while(t.size())
{
int now=t.front();
t.pop();
inq[now]=0;
for(int i=0;i<p[now].size();i++)
{
int to=p[now][i].v,cost=p[now][i].w;
// cout<<"to "<<to<<" w "<<cost<<endl;
if(dis[to]>dis[now]+cost)
{
dis[to]=dis[now]+cost;
// cout<<"到"<<to<<" "<<dis[to]<<"cost "<<cost<<endl;
cnt[to]=cnt[now]+1;
if(cnt[to]>=np)//有负权回路,把跟他连通的都标记出来,定为1
{
queue<int> tem;//广搜找到与他连通的点;
int vis[210];
for(int ii=0;ii<=np;ii++) vis[ii]=0;
vis[to]=1;
tem.push(to);
ino[to]=1;
while(tem.size())
{
int tt=tem.front();
tem.pop();
for(int j=0;j<p[tt].size();j++)
{
int ttem=p[tt][j].v;
ino[ttem]=1;
if(vis[ttem]==0)
{
vis[ttem]=1;
tem.push(ttem);
}
}
}
}
if((inq[to]==0)&&(ino[to]==0))
{
t.push(to);
inq[to]=1;
}
}
}
}
cout<<"Case "<<ci<<":"<<endl;
int nq;//问题个数
cin>>nq;
for(int i=0;i<nq;i++)
{
int pp;
cin>>pp;
int diss=dis[pp];
if((diss<3)||(diss==1000000000)||(ino[pp]==1)) cout<<"?"<<endl;
else cout<<diss<<endl;
}
}
return 0;
}