HNAU 19th Training Problem 第19次训练 2013 /09 /28 AMA.模拟 poj 1455 B.博弈 hdu 2177 C.数学 poj 1710 D.搜索 poj 2362 E.贪心 poj 1323 F.图论 poj 2387 G.矩阵 poj 1588 H.动态规划 poj 1170 |
E.贪心
题意:这道题还蛮有意思的,M个人,每人N张牌,每轮比较谁出的牌大,最大者为胜。现在给定M和N,以及你的牌,要求输出你至少能确保获得几轮的胜利。
下面看一下第二个样例:
6 11
62 63 54 66 65 61 57 56 50 53 48
首先对手中的牌做一个降序排列,即
66 65 63 62 61 57 56 54 53 50 48
66和65肯定必胜,63因为外面有64,所以不能保证必胜,62 64必胜一个(因为外面只有一个63消耗)
我的策略是每次出手上最大的牌。看至少能赢几次。
注意:这里应注意循环模拟 n次出牌;
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
int vis[2002];
int main()
{
int num,i,n,round;
int cnt=1;
while(~scanf("%d%d",&n,&round))
{
if(n==0&&round==0)
break;
int maxn=n*round;
memset(vis,0,sizeof(vis));
for(i=0; i<round; i++)
{
scanf("%d",&num);
vis[num]=1;
}
int other=0;
int ans=0;
//66 65 63 62 61 57 56 54 53 50 48
for(i=0; i<round&& maxn>0;maxn--)
{
if(vis[maxn])
{
i++;//i++必须写在,因为我每次拿出自己最大的一个数出来,总共拿n次
if(other==0)
ans++;
else
other--;
}
else//统计前方有多少个需要消耗
other++;
}
printf("Case %d: %d\n",cnt++,ans);
}
return 0;
}
/**
3 4
12 10 9 8
ans=3
*/
F .图论
裸的最短路,用spfa:
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <queue>
using namespace std;
#define M 1002
#define INF 1<<30
struct edge
{
int to,v;
edge() {}
edge(int a,int b):to(a),v(b) {}
};
vector<edge> E[M];//n point
int mincost;
int n;
void init()
{
for(int i=1; i<=n; i++)
E[i].clear();
}
void add(int a,int b,int c)
{
E[a].push_back(edge(b,c));
E[b].push_back(edge(a,c));
}
int spfa(int s,int e)
{
int vis[M];
memset(vis,0,sizeof(vis));
int dist[M];
int i,v;
for(i=0; i<=n; i++)
dist[i]=INF;
dist[s]=0;
queue<int> Q;
Q.push(s);
while(!Q.empty())
{
v=Q.front();
Q.pop();
vis[v]=0;
for(i=0; i<E[v].size(); i++)
{
int j=E[v][i].to;
if(dist[v]+E[v][i].v<dist[j])
{
dist[j]=dist[v]+E[v][i].v;
if(!vis[j])
{
Q.push(j);
vis[j]=1;
}
}
}
}
return dist[e];
}
int main()
{
int t,i,a,b,c;
while(~scanf("%d%d",&t,&n))
{
init();
for(i=0; i<t; i++)
{
scanf("%d%d%d",&a,&b,&c);
add(a,b,c);
}
printf("%d\n",spfa(n,1));
}
return 0;
}