1069: Donald’s company (最小生成树)
时间限制: 1 Sec 内存限制: 128 MBhttp://acm.xidian.edu.cn/problem.php?id=1069
[ 提交 ][ 状态 ][ 讨论版 ]
题目描述
输入
输出
样例输入
3
2 2 50 2
0 1 10
1 0 20
2 2 100 4
0 0 10
0 1 20
1 0 30
1 1 40
4 3 10 6
0 2 2
1 2 4
3 1 5
2 0 1
1 1 3
3 2 2
样例输出
170
310
55
题目大意:有一个公司要雇佣m个男孩,n个女孩,每个人的费用为k元,且有x种关系:(i,j,r),表示若男孩i或女孩j已被雇佣,则雇佣另一个人只需要k-r元,求最小花费?
刚开始以为是有向图,用最小树形图超时了。。。仔细一看是无向图,就直接用最小生成树即可
建立一个根节点0,到每个点都有权值为k的边,然后按照题目意思建立边即可
#include <cstdio>
#include <cstring>
#include <queue>
#include <vector>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
struct Edge{
int v,w;//v表示边端点,另一个端点已知;w表示边权值,也表示v到最小生成树的距离
Edge(int vv=0,int ww=INF):v(vv),w(ww) {}
bool operator < (const Edge& a) const {
return w>a.w;
}
}u;
int m,n,dis[20005];//dis表示各顶点到最小生成树的距离
bool vis[20005];//vis表示各顶点是否已被加入最小生成树
vector<Edge> g[20005];//邻接表
int prim() {//prim算法求最小生成树
int i,j,v,w,ans=0,cnt=0;
priority_queue<Edge> q;
q.push(Edge(0,0));
cnt=0;
memset(vis,false,sizeof(vis));
memset(dis,0x3f,sizeof(dis));
while(cnt<n&&!q.empty()) {
do {
u=q.top();
q.pop();
}while(vis[u.v]&&!q.empty());
if(!vis[u.v]) {
++cnt;
ans+=u.w;
vis[u.v]=true;
for(i=0,j=g[u.v].size();i<j;++i) {
if(!vis[v=g[u.v][i].v]&&dis[v]>(w=g[u.v][i].w)) {
dis[v]=w;
q.push(Edge(v,w));
}
}
}
}
return ans;
}
int main() {
int T,s,e,r,M,N,K,X;
scanf("%d",&T);
while(T-->0) {
scanf("%d%d%d%d",&M,&N,&K,&X);
n=m=M+N;
for(int i=0;i<=m;++i) {
g[i].clear();
}
for(int i=m;i>0;--i) {
g[0].push_back(Edge(i,K));
}
while(X-->0) {
scanf("%d%d%d",&s,&e,&r);
s+=1;
e+=M+1;
g[s].push_back(Edge(e,K-r));
g[e].push_back(Edge(s,K-r));
}
printf("%d\n",prim());
}
return 0;
}
1071: Rectangle Counting (枚举)
时间限制: 1 Sec 内存限制: 128 MBhttp://acm.xidian.edu.cn/problem.php?id=1071
[ 提交 ][ 状态 ][ 讨论版 ]
题目描述
输入
输出
样例输入
2
1 1
2 2
样例输出
1
9
题目大意:有n*m的格子,求共有多少个矩形?
枚举矩形的右下角,则以(i,j)为右下角的矩形有i*j个
#include <cstdio>
#include <cstring>
using namespace std;
int T,n,m,ans;
int main() {
scanf("%d",&T);
while(T-->0) {
scanf("%d%d",&n,&m);
ans=0;
for(int i=n;i>0;--i) {
for(int j=m;j>0;--j) {
ans+=i*j;
}
}
printf("%d\n",ans);
}
return 0;
}
1072: National Disaster (Tarjan)
时间限制: 1 Sec 内存限制: 128 MBhttp://acm.xidian.edu.cn/problem.php?id=1072
[ 提交 ][ 状态 ][ 讨论版 ]
题目描述
输入
输出
样例输入
2
2 1
0 1
3 3
0 1
1 2
2 0
样例输出
1
0
题目大意:求无向图中桥的个数?
直接用Tarjan即可
#include <cstdio>
#include <cstring>
#include <vector>
using namespace std;
int n,m,cnt;
int dfn[10005],low[10005],num;
vector<int> g[10005];
void Tarjan(int u,int p) {
dfn[u]=low[u]=++num;
int v;
for(int i=0;i<g[u].size();++i) {
v=g[u][i];
if(v!=p) {
if(dfn[v]==0) {
Tarjan(v,u);
low[u]=min(low[u],low[v]);
if(dfn[u]<low[v]) {
++cnt;
}
}
else {
low[u]=min(low[u],dfn[v]);
}
}
}
}
int main() {
int T,s,e;
scanf("%d",&T);
while(T-->0) {
scanf("%d%d",&n,&m);
for(int i=0;i<n;++i) {
g[i].clear();
dfn[i]=0;
}
while(m-->0) {
scanf("%d%d",&s,&e);
g[s].push_back(e);
g[e].push_back(s);
}
num=cnt=0;
Tarjan(0,-1);
printf("%d\n",cnt);
}
return 0;
}
1073: Nunchakus (DP)
时间限制: 1 Sec 内存限制: 128 MBhttp://acm.xidian.edu.cn/problem.php?id=1073
[ 提交 ][ 状态 ][ 讨论版 ]
题目描述
输入
输出
样例输入
3
2
1 1
3
1 2 3
3
1 1 3
样例输出
Yes
Yes
No
题目大意:有n个木棍,是否能分成两堆,使两堆的木棍长度和相等?
01背包,看这些木棍能组成哪些长度,若sum为偶数且sum>>1可以组成,则输出Yes,否则输出No
#include <cstdio>
#include <cstring>
using namespace std;
int T,n;
int l,sum;
bool len[6005];
int main() {
scanf("%d",&T);
while(T-->0) {
scanf("%d",&n);
sum=0;
memset(len,false,sizeof(len));
len[0]=true;
while(n-->0) {
scanf("%d",&l);
sum+=l;
for(int i=5000-l;i>=0;--i) {
if(len[i]) {
len[i+l]=true;
}
}
}
printf("%s\n",(sum&1)==0?(len[sum>>1]?"Yes":"No"):("No"));
}
return 0;
}