hdu-2647 Reward && hdu-2049产生冠军 &&hdu-3342Legal or Not(拓扑排序)
题目链接:
/*Problem : 2647 ( Reward ) Judge Status : Accepted
RunId : 16919085 Language : G++ Author : 2014300227
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const ll mod=1e9+;
int n,m,ind[N],u,v,vis[N];
vector<int>ve[N];
struct node
{
int num,pri;
};
node temp;
queue<node>qu;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
ll ans=;
for(int i=;i<=n;i++)
{
ve[i].clear();
vis[i]=ind[i]=;
}
for(int i=;i<m;i++)
{
scanf("%d%d",&u,&v);
ve[v].push_back(u);
ind[u]++;
}
for(int i=;i<=n;i++)
{
if(!ind[i])
{
temp.num=i;
temp.pri=;
qu.push(temp);
}
}
int cnt=n;
while(!qu.empty())
{
node fr=qu.front();
ans+=(ll)fr.pri;
qu.pop();
cnt--;
int len=ve[fr.num].size();
for(int i=;i<len;i++)
{
int y=ve[fr.num][i];
ind[y]--;
if(ind[y]==)
{
temp.num=y;
temp.pri=fr.pri+;
qu.push(temp);
}
}
}
if(cnt==)printf("%lld\n",ans);
else printf("-1\n");
}
return ;
}
/*Problem : 2094 ( 产生冠军 ) Judge Status : Accepted
RunId : 16919272 Language : G++ Author : 2014300227
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const ll mod=1e9+;
int n,ind[N];
map<string,int>mp;
vector<int>ve[N];
string str1[N],str2[N];
int main()
{
while()
{
scanf("%d",&n);
if(n==)break;
for(int i=;i<=n;i++)
{
ve[i].clear();
ind[i]=;
}
int cnt=;
for(int i=;i<n;i++)
{
cin>>str1[i]>>str2[i];
if(mp[str1[i]]==)mp[str1[i]]=cnt++;
if(mp[str2[i]]==)mp[str2[i]]=cnt++;
int u=mp[str1[i]],v=mp[str2[i]];
ve[u].push_back(v);
ind[v]++;
}
int num=;
for(int i=;i<cnt;i++)
{
if(!ind[i])
{
num++;
}
}
if(num!=)printf("No\n");
else printf("Yes\n");
for(int i=;i<n;i++)
{
mp[str1[i]]=;
mp[str2[i]]=;
} }
return ;
}
/*Problem : 2647 ( Reward ) Judge Status : Accepted
RunId : 16919085 Language : G++ Author : 2014300227
Code Render Status : Rendered By HDOJ G++ Code Render Version 0.01 Beta*/
#include <bits/stdc++.h>
using namespace std;
const int N=1e4+;
typedef long long ll;
const ll mod=1e9+;
int n,m,ind[N],u,v,vis[N];
vector<int>ve[N];
struct node
{
int num,pri;
};
node temp;
queue<node>qu;
int main()
{
while(scanf("%d%d",&n,&m)!=EOF)
{
ll ans=;
for(int i=;i<=n;i++)
{
ve[i].clear();
vis[i]=ind[i]=;
}
for(int i=;i<m;i++)
{
scanf("%d%d",&u,&v);
ve[v].push_back(u);
ind[u]++;
}
for(int i=;i<=n;i++)
{
if(!ind[i])
{
temp.num=i;
temp.pri=;
qu.push(temp);
}
}
int cnt=n;
while(!qu.empty())
{
node fr=qu.front();
ans+=(ll)fr.pri;
qu.pop();
cnt--;
int len=ve[fr.num].size();
for(int i=;i<len;i++)
{
int y=ve[fr.num][i];
ind[y]--;
if(ind[y]==)
{
temp.num=y;
temp.pri=fr.pri+;
qu.push(temp);
}
}
}
if(cnt==)printf("%lld\n",ans);
else printf("-1\n");
}
return ;
}
hdu-2647 Reward && hdu-2049产生冠军 &&hdu-3342Legal or Not(拓扑排序)的更多相关文章
- ACM: hdu 2647 Reward -拓扑排序
hdu 2647 Reward Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Des ...
- HDU.2647 Reward(拓扑排序 TopSort)
HDU.2647 Reward(拓扑排序 TopSort) 题意分析 裸的拓扑排序 详解请移步 算法学习 拓扑排序(TopSort) 这道题有一点变化是要求计算最后的金钱数.最少金钱值是888,最少的 ...
- 题解报告:hdu 2647 Reward(拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 Problem Description Dandelion's uncle is a boss ...
- hdu 2647 Reward
题目连接 http://acm.hdu.edu.cn/showproblem.php?pid=2647 Reward Description Dandelion's uncle is a boss o ...
- HDU 2647 Reward (拓扑排序)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题意是给你n点m条有向边,叶子点(出度为0)上的值为888,父亲点为888+1,依次计算... ...
- HDU 2647 Reward(拓扑排序+判断环+分层)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2647 题目大意:要给n个人发工资,告诉你m个关系,给出m行每行a b,表示b的工资小于a的工资,最低工 ...
- HDU 2647 Reward【反向拓扑排序】
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Submis ...
- HDU——2647 Reward
Reward Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total Sub ...
- HDU 2647 Reward(图论-拓扑排序)
Reward Problem Description Dandelion's uncle is a boss of a factory. As the spring festival is comin ...
- hdu 2647 Reward(拓扑排序,反着来)
Reward Time Limit : 2000/1000ms (Java/Other) Memory Limit : 32768/32768K (Java/Other) Total Submis ...
随机推荐
- js和jquery 实现网站来消息网站标题闪动提示
js版 <html xmlns="http://www.w3.org/1999/xhtml" > <head runat="server"&g ...
- [Algorithm] Linked List Data Structure in JavaScript
A linked list is a collection of items where each item points to the next one in the list. Because o ...
- Android 设定横屏,禁止屏幕旋转,Activity重置 [更新视频播放器相关]
1. 设定屏幕方向 当指定了屏幕的方向后(非SCREEN_ORIENTATION_UNSPECIFIED),屏幕就不会自己主动的旋转了 有2中方式控制屏幕方向: 1.1 改动AndroidManife ...
- 【Scala类型系统】自身类型(self type)引用
定义 特质能够要求混入它的类扩展自还有一个类型,可是当使用自身类型(self type)的声明来定义特质时(this: ClassName =>).这种特质仅仅能被混入给定类型的子类其中. 如果 ...
- 以goroutine为例看协程的相关概念
转自 http://wangzhezhe.github.io/blog/2016/02/17/golang-scheduler/ 基本上是网上相关文章的梳理,初衷主要是想了解下golang中的gor ...
- soapUI学习笔记--用例字段参数化
字段参数化的简单操作 1.把Request新增一个TestCase 增加TestCase,下方会出现: 2.案例中,请求参数只有一个.先运行下请求,可以运行成功(保证接口是通的) 3.添加参数.见图中 ...
- AMD单双桥时序简叙
芯片组(双桥)时序 VBAT :RTC电路的供电3V(RTC电路有问题会导致没复位或不跑码等故障) RTCCLK :晶振起振给南桥提供32.768KHz频率(RTC电路有问题会导致没复位或不跑码等故障 ...
- ACPI in Linux
https://01.org/zh/linux-acpi The goal of this project is to enable Linux to take advantage of platfo ...
- ZOJ 3156 Taxi (二分匹配+二分查找)
题目链接:Taxi Taxi Time Limit: 1 Second Memory Limit: 32768 KB As we all know, it often rains sudde ...
- MyBatis学习(二):与Spring整合(非注解方式配置MyBatis)
搭建SpringMVC的-->传送门<-- 一.环境搭建: 目录结构: 引用的JAR包: 如果是Maven搭建的话,pom.xml的配置如下: <?xml version=" ...