Genealogical tree
| Time Limit: 1000MS |
| Memory Limit: 65536K | ||
| Total Submissions: 4365 |
| Accepted: 2902 |
| Special Judge |
Description
The system of Martians' blood relations is confusing enough. Actually, Martians bud when they want and where they want. They gather together in different groups, so that a Martian can have one parent as well as ten. Nobody will be surprised by a hundred of children. Martians have got used to this and their style of life seems to them natural.
And in the Planetary Council the confusing genealogical system leads to some embarrassment. There meet the worthiest of Martians, and therefore in order to offend nobody in all of the discussions it is used first to give the floor to the old Martians, than to the younger ones and only than to the most young childless assessors. However, the maintenance of this order really is not a trivial task. Not always Martian knows all of his parents (and there's nothing to tell about his grandparents!). But if by a mistake first speak a grandson and only than his young appearing great-grandfather, this is a real scandal.
Your task is to write a program, which would define once and for all, an order that would guarantee that every member of the Council takes the floor earlier than each of his descendants.
Input
The first line of the standard input contains an only number N, 1 <= N <= 100 — a number of members of the Martian Planetary Council. According to the centuries-old tradition members of the Council are enumerated with the natural numbers from 1 up to N. Further, there are exactly N lines, moreover, the I-th line contains a list of I-th member's children. The list of children is a sequence of serial numbers of children in a arbitrary order separated by spaces. The list of children may be empty. The list (even if it is empty) ends with 0.
Output
The standard output should contain in its only line a sequence of speakers' numbers, separated by spaces. If several sequences satisfy the conditions of the problem, you are to write to the standard output any of them. At least one such sequence always exists.
Sample Input
5
0
4 5 1 0
1 0
5 3 0
3 0
Sample Output
2 4 5 3 1
Source
Ural State University Internal Contest October'2000 Junior Session
题目大意:起点是编号为0的节点,其余有n个发电站节点,有m条无向边,每次可以派出一辆坦克占领一个发电站,当占领的发电站的点权值和大于总点权值的一半的时候,就是达到了目的的时候,问最少油量消耗,油量消耗==路径和。
例如样例,派出了一辆坦克,直接占领了2号油田,获得3点权值,3>4/2,所以就是输出最优解5.第二个样例因为图不连通,所以输出impossible。
思路:Dij求最短路,因为我们要求的是最小油量花费,其实也就是最小路径花费,其实就相当于一个01背包,这个点派出坦克与否的选择,所以我们要将单源最短路径的路径权值设定为背包要装入物品的价值,将点权值设定为花费即可,维护最小花费,直接01背包动态规划搞定这个问题即可,然后遍历DP数组,从V/2+1遍历到V,找最小花费。
AC代码:
/*
单源最短路+01背包;
*/
#include<stdio.h>
#include<string.h>
#include<iostream>
using namespace std;
struct pack
{
int w,val;
}a[10000];
struct EdgeNode
{
int to;
int w;
int next;
}e[100000];
int head[1000];//
int vis[1000];//
int dis[1000];//
int price[1000];//
int dp[1200005];//
int n,m,cont,V;
int min(int a,int b)
{
return a<b?a:b;
}
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void DP()
{
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=V;j>=a[i].w;j--)
{
dp[j]=min(dp[j],dp[j-a[i].w]+a[i].val);
}
}
int output=0x3f3f3f3f;
for(int i=V/2+1;i<=V;i++)
{
output=min(output,dp[i]);
}
if(output==0x3f3f3f3f)
{
printf("impossible\n");
}
else printf("%d\n",output);
}
void Dij(int ss)
{
memset(vis,0,sizeof(vis));
for(int i=0;i<=n;i++)dis[i]=0x3f3f3f3f;
dis[ss]=0;
for(int i=0;i<n+1;i++)
{
int tmp=0x3f3f3f3f;
int k=-1;
for(int j=0;j<=n;j++)
{
if(dis[j]<tmp&&vis[j]==0)
{
tmp=dis[j];
k=j;
}
}
vis[k]=1;
for(int j=head[k];j!=-1;j=e[j].next)
{
int v=e[j].to;
int w=e[j].w;
if(vis[v]==0&&dis[v]>dis[k]+w)
{
dis[v]=dis[k]+w;
}
}
}
for(int i=1;i<=n;i++)
{
a[i].w=price[i];
a[i].val=dis[i];
}
DP();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
V=0;
cont=0;
memset(dp,0x3f3f3f3f,sizeof(dp));
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&price[i]);
V+=price[i];
}
Dij(0);
}
}
Dij+优先队列优化AC:
#include<stdio.h>
#include<string.h>
#include<iostream>
#include<queue>
using namespace std;
struct node
{
int d,u;
friend bool operator <(node a,node b)
{
return a.d>b.d;
}
}now,nex;
struct pack
{
int w,val;
}a[10000];
struct EdgeNode
{
int to;
int w;
int next;
}e[100000];
int head[1000];//
int dis[1000];//
int price[1000];//
int dp[1200005];//
int n,m,cont,V;
int min(int a,int b)
{
return a<b?a:b;
}
void add(int from,int to,int w)
{
e[cont].to=to;
e[cont].w=w;
e[cont].next=head[from];
head[from]=cont++;
}
void DP()
{
dp[0]=0;
for(int i=1;i<=n;i++)
{
for(int j=V;j>=a[i].w;j--)
{
dp[j]=min(dp[j],dp[j-a[i].w]+a[i].val);
}
}
int output=0x3f3f3f3f;
for(int i=V/2+1;i<=V;i++)
{
output=min(output,dp[i]);
}
if(output==0x3f3f3f3f)
{
printf("impossible\n");
}
else printf("%d\n",output);
}
void Priority_Dij(int ss)
{
for(int i=1;i<=n;i++)dis[i]=0x3f3f3f3f;
dis[ss]=0;
priority_queue<node>s;
now.u=ss;
now.d=0;
s.push(now);
while(!s.empty())
{
now=s.top();
s.pop();
int u=now.u;
for(int k=head[u];k!=-1;k=e[k].next)
{
int v=e[k].to;
int w=e[k].w;
if(dis[v]>dis[u]+w)
{
dis[v]=dis[u]+w;
nex.u=v;
nex.d=dis[v];
s.push(nex);
}
}
}
for(int i=1;i<=n;i++)
{
a[i].w=price[i];
a[i].val=dis[i];
}
DP();
}
int main()
{
int t;
scanf("%d",&t);
while(t--)
{
V=0;
cont=0;
memset(dp,0x3f3f3f3f,sizeof(dp));
memset(head,-1,sizeof(head));
scanf("%d%d",&n,&m);
for(int i=0;i<m;i++)
{
int x,y,w;
scanf("%d%d%d",&x,&y,&w);
add(x,y,w);
add(y,x,w);
}
for(int i=1;i<=n;i++)
{
scanf("%d",&price[i]);
V+=price[i];
}
Priority_Dij(0);
}
}

本文介绍了一个关于火星议会成员发言顺序的问题,通过构建基因alogy树来确保年长者优先发言,避免社会尴尬。采用特定的数据结构和算法解决这一问题。
521

被折叠的 条评论
为什么被折叠?



