目录
A1003 Emergency(点权)
PAT测试显示答案错误+段错误,可是我实在找不到哪还有问题,求指出
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=510;
const int INF=1000000000;
int n,m,st,ed,G[maxn][maxn],weight[maxn];
int d[maxn],w[maxn],num[maxn];
bool vis[maxn]={false};
void Dijkstra(int s)
{
fill(d,d+maxn,INF);
memset(num,0,sizeof(num));
memset(w,0,sizeof(w));
d[s]=0;
w[s]=weight[s];
num[s]=1;
for(int i=0;i<n;i++)
{
int u=-1,MIN=INF;
for(int j=0;j<n;j++)
{
if(vis[j]==false&&d[j]<MIN)
{
u=j;
MIN=d[j];
}
}
if(u==-1)
return;
vis[u]=true;
for(int v=0;v<n;v++)
{
if(vis[v]==false&&G[u][v]!=INF)
{
if(d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v];
w[v]=w[u]+weight[v];
num[v]=num[u];
}
else if(d[u]+G[u][v]==d[v])
{
if(w[u]+weight[v]>w[v])
{
w[v]=w[u]+weight[v];
}
num[v]+=num[u];
}
}
}
}
}
int main()
{
cin>>n>>m>>st>>ed;
for(int i=0;i<n;i++)
{
scanf("%d",&weight[i]);
}
int u,v;
fill(G[0],G[0]+maxn*maxn,INF);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
scanf("%d",&G[u][v]);
G[v][u]=G[u][v];
}
Dijkstra(st);
printf("%d %d\n",num[ed],w[ed]);
return 0;
}
A1020 Tree Traversals
#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
#include<queue>
using namespace std;
const int maxn=50;
struct node{
int data;
node* lchild;
node* rchild;
};
int pre[maxn],in[maxn],post[maxn];
int n;
node* create(int postL,int postR,int inL,int inR){
if(postL>postR)
{
return NULL;
}
node* root=new node;
root->data=post[postR];
int k;
for(k=inL;k<=inR;k++)
{
if(in[k]==post[postR])
{
break;
}
}
int numLeft=k-inL;
root->lchild=create(postL,postL+numLeft-1,inL,k-1);
root->rchild=create(postL+numLeft,postR-1,k+1,inR);
return root;
}
int num=0;
void BFS(node* root)
{
queue<node*> q;
q.push(root);
while(!q.empty())
{
node* now=q.front();
q.pop();
printf("%d",now->data);
num++;
if(num<n)
printf(" ");
if(now->lchild!=NULL)
q.push(now->lchild);
if(now->rchild!=NULL)
q.push(now->rchild);
}
}
int main(){
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&post[i]);
}
for(int i=0;i<n;i++)
{
scanf("%d",&in[i]);
}
node* root=create(0,n-1,0,n-1);
BFS(root);
return 0;
}
A1030 Travel Plan(边权)
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<queue>
#include<algorithm>
using namespace std;
const int maxn=510;
const int INF=1000000000;
int n,m,st,ed,G[maxn][maxn],weight[maxn][maxn];
int d[maxn],w[maxn],pre[maxn];
bool vis[maxn]={false};
void Dijkstra(int s)
{
fill(d,d+maxn,INF);
fill(w,w+maxn,INF);
for(int i=0;i<n;i++)
{
pre[i]=i;
}
d[s]=0;
w[s]=0;
for(int i=0;i<n;i++)
{
int u=-1,MIN=INF;
for(int j=0;j<n;j++)
{
if(vis[j]==false&&d[j]<MIN)
{
u=j;
MIN=d[j];
}
}
if(u==-1)
return;
vis[u]=true;
for(int v=0;v<n;v++)
{
if(vis[v]==false&&G[u][v]!=INF)
{
if(d[u]+G[u][v]<d[v])
{
d[v]=d[u]+G[u][v];
w[v]=w[u]+weight[u][v];
pre[v]=u;
}
else if(d[u]+G[u][v]==d[v])
{
if(w[u]+weight[u][v]<w[v])
{
w[v]=w[u]+weight[u][v];
pre[v]=u;
}
}
}
}
}
}
void DFS(int v)
{
if(v==st)
{
printf("%d ",v);
return;
}
DFS(pre[v]);
printf("%d ",v);
}
int main()
{
cin>>n>>m>>st>>ed;
int u,v;
fill(G[0],G[0]+maxn*maxn,INF);
for(int i=0;i<m;i++)
{
scanf("%d%d",&u,&v);
scanf("%d%d",&G[u][v],&weight[u][v]);
G[v][u]=G[u][v];
weight[v][u]=weight[u][v];
}
Dijkstra(st);
DFS(ed);
printf("%d %d\n",d[ed],w[ed]);
return 0;
}
A1032 Sharing
#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=100010;
struct NODE{
char data;
int next;
bool flag;
}node[maxn];
int main(){
for(int i=0;i<maxn;i++){
node[i].flag=false;
}
int s1,s2,n,p;
scanf("%d%d%d",&s1,&s2,&n);
int address,next;
char data;
for(int i=0;i<n;i++){
scanf("%d %c %d",&address,&data,&next);
node[address].data=data;
node[address].next=next;
}
for(p=s1;p!=-1;p=node[p].next){
node[p].flag=true;
}
for(p=s2;p!=-1;p=node[p].next){
if(node[p].flag==true)
break;
}
if(p!=-1){
printf("%05d\n",p);
}
else{
printf("-1\n");
}
return 0;
}
A1034 Head of a Gang
#include<iostream>
#include<cstring>
#include<map>
#include<string>
using namespace std;
const int maxn=2010;
const int INF=1000000000;
map<int,string> inttostring;
map<string,int> stringtoint;
map<string,int> gang;
int G[maxn][maxn]={0};
int weight[maxn]={0};
int n,k,numperson=0;
bool vis[maxn]={false};
void DFS(int nowvist,int& head,int& nummember,int &totalvalue)
{
nummember++;
vis[nowvist]=true;
if(weight[nowvist]>weight[head])
{
head=nowvist;
}
for(int i=0;i<numperson;i++)
{
if(G[nowvist][i]>0)
{
totalvalue+=G[nowvist][i];
G[nowvist][i]=G[i][nowvist]=0;
if(vis[i]==false)
{
DFS(i,head,nummember,totalvalue);
}
}
}
}
void DFSTrave()
{
for(int i=0;i<numperson;i++)
{
if(vis[i]==false)
{
int head=i,nummember=0,totalvalue=0;
DFS(i,head,nummember,totalvalue);
if(nummember>2&&totalvalue>k)
{
gang[inttostring[head]]=nummember;
}
}
}
}
int change(string str)
{
if(stringtoint.find(str)!=stringtoint.end())
{
return stringtoint[str];
}
else
{
stringtoint[str]=numperson;
inttostring[numperson]=str;
return numperson++;
}
}
int main(){
int w;
string str1,str2;
cin>>n>>k;
for(int i=0;i<n;i++)
{
cin>>str1>>str2>>w;
int id1=change(str1);
int id2=change(str2);
weight[id1]+=w;
weight[id2]+=w;
G[id1][id2]+=w;
G[id2][id1]+=w;
}
DFSTrave();
cout<<gang.size()<<endl;
map<string,int>::iterator it;
for(it=gang.begin();it!=gang.end();it++)
{
cout<<it->first<<" "<<it->second<<endl;
}
return 0;
}
A1043 Is it a Binary Search Tree
测试时显示部分正确,不知道为什么
#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
#include<queue>
#include<vector>
using namespace std;
const int maxn=110;
struct node{
int data;
node *left,*right;
};
void insert(node* &root,int data)
{
if(root==NULL)
{
root=new node;
root->data=data;
root->left=root->right=NULL;
return;
}
if(data<root->data)
insert(root->left,data);
else
insert(root->right,data);
}
void preorder(node* root,vector<int>&vi)
{
if(root==NULL)
return;
vi.push_back(root->data);
preorder(root->left,vi);
preorder(root->right,vi);
}
void preordermirror(node* root,vector<int>&vi)
{
if(root==NULL)
return;
vi.push_back(root->data);
preordermirror(root->right,vi);
preordermirror(root->left,vi);
}
void postorder(node* root,vector<int>&vi)
{
if(root==NULL)
return;
postorder(root->left,vi);
postorder(root->right,vi);
vi.push_back(root->data);
}
void postordermirror(node* root,vector<int>&vi)
{
if(root==NULL)
return;
postordermirror(root->right,vi);
postordermirror(root->left,vi);
vi.push_back(root->data);
}
vector<int> origin,pre,preM,post,postM;
int main(){
int n,data;
node* root=NULL;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d",&data);
origin.push_back(data);
insert(root,data);
}
preorder(root,pre);
preordermirror(root,preM);
postorder(root,post);
postordermirror(root,postM);
if(origin==pre)
{
printf("YES\n");
for(int i=0;i<post.size();i++)
{
printf("%d",post[i]);
if(i<post.size()-1)
printf(" ");
}
}
else if(origin==preM)
{
printf("YES\n");
for(int i=0;i<postM.size();i++)
{
printf("%d",postM[i]);
if(i<postM.size()-1)
printf(" ");
}
}
else
{
printf("No\n");
}
return 0;
}
A1052 Linked List Sorting
#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
using namespace std;
const int maxn=100005;
struct NODE{
int data;
int next;
int address;
bool flag;
}node[maxn];
bool cmp(NODE a,NODE b){
if(a.flag==false||b.flag==false){
return a.flag>b.flag;
}
else{
return a.data<b.data;
}
}
int main(){
for(int i=0;i<maxn;i++){
node[i].flag=false;
}
int n,begin,address;
scanf("%d%d",&n,&begin);
for(int i=0;i<n;i++){
scanf("%d",&address);
scanf("%d%d",&node[address].data,&node[address].next);
node[address].address=address;
// node[address].next=next;
}
int count=0,p;
for(p=begin;p!=-1;p=node[p].next){
node[p].flag=true;
count++;
}
if(count==0){
printf("0 -1");
}
else{
sort(node,node+maxn,cmp);
printf("%d %05d\n",count,node[0].address);
for(int i=0;i<count;i++)
{
if(i!=count-1)
{
printf("%05d %d %05d\n",node[i].address,node[i].data,node[i+1].address);
}
else
{
printf("%05d %d -1\n",node[i].address,node[i].data);
}
}
}
return 0;
}
A 1053
#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
#include<queue>
#include<vector>
using namespace std;
const int maxn=110;
struct node{
int weight;
vector<int> child;
}Node[maxn];
bool cmp(int a,int b)
{
return Node[a].weight>Node[b].weight;
}
int path[maxn];
int n,m,s;
void DFS(int index,int numNode,int sum)
{
if(sum>s)
return;
if(sum==s)
{
if(Node[index].child.size()!=0)
return;
}
for(int i=0;i<numNode;i++)
{
printf("%d",Node[path[i]].weight);
if(i<numNode-1)
printf(" ");
else
printf("\n");
return;
}
for(int i=0;i<Node[index].child.size();i++)
{
int child=Node[index].child[i];
path[numNode]=child;
DFS(child,numNode+1,sum+Node[child].weight);
}
}
int main(){
scanf("%d%d%d",&n,&m,&s);
for(int i=0;i<n;i++)
{
scanf("%d",&Node[i].weight);
}
int id,k,child;
for(int i=0;i<m;i++)
{
scanf("%d%d",&id,&child);
for(int j=0;j<k;j++)
{
scanf("%d",&child);
Node[id].child.push_back(child);
}
sort(Node[id].child.begin(), Node[id].child.end(), cmp);
}
path[0]=0;
DFS(0,1,Node[0].weight);
return 0;
}
A1060 Are They Equal
#include <cstring>
#include<cstdio>
#include<algorithm>
#include<math.h>
#include <iostream>
using namespace std;
int n;
string deal(string s,int& e){
int k=0;
while(s.length()>0&&s[0]=='0'){
s.erase(s.begin());
}
if(s[0]=='.'){
s.erase(s.begin());
while(s.length()>0&&s[0]=='0'){
s.erase(s.begin());
e--;
}
}
else{
while(k<s.length()&&s[k]!='.'){
k++;
e++;
}
if(k<s.length()){
s.erase(s.begin()+k);
}
}
if(s.length()==0){
e=0;
}
int num=0;
k=0;
string res;
while(num<n){
if(k<s.length())
res+=s[k++];
else
res+='0';
num++;
}
return res;
}
int main(){
string s1,s2,s3,s4;
cin>>n>>s1>>s2;
int e1=0,e2=0;
s3=deal(s1,e1);
s4=deal(s2,e2);
if(s3==s4&&e1==e2){
cout<<"YES 0."<<s3<<"*10^"<<e1<<endl;
}else{
cout<<"NO 0."<<s3<<"*10^"<<e1<<" 0."<<s4<<"*10^"<<e2<<endl;
}
return 0;
}
A1076 Forwards on Weibo
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#include<string>
#include<vector>
#include<queue>
using namespace std;
const int maxn=1010;
struct node{
int id;
int layer;
};
vector<node> Adj[maxn];
bool inq[maxn]={false};
int BFS(int s,int L)
{
int numforword=0;
queue<node> q;
node start;
start.id=s;
start.layer=0;
q.push(start);
inq[start.id]=true;
while(!q.empty())
{
node topnode=q.front();
q.pop();
int u=topnode.id;
for(int i=0;i<Adj[u].size();i++)
{
node next=Adj[u][i];
next.layer=topnode.layer+1;
if(inq[next.id]==false&&next.layer<=L)
{
q.push(next);
inq[next.id]=true;
numforword++;
}
}
}
return numforword;
}
int main(){
node user;
int n,L,numfollow,idfollow;
cin>>n>>L;
for(int i=1;i<=n;i++)
{
user.id=i;
cin>>numfollow;
for(int j=0;j<numfollow;j++)
{
scanf("%d",&idfollow);
Adj[idfollow].push_back(user);
}
}
int numquery,s;
scanf("%d",&numquery);
for(int i=0;i<numquery;i++)
{
memset(inq,false,sizeof(inq));
scanf("%d",&s);
int numforword=BFS(s,L);
printf("%d\n",numforword);
}
return 0;
}