您的位置:首页 > 其它

[贪心]UVA10720 - Graph Construction

2014-04-06 11:43 232 查看
Problem C
Graph Construction
Time Limit
2 Seconds
Graph is a collection of edges E and vertices V. Graph has a wide variety of applications in computer. There are different ways to represent graph in computer.
It can be represented by adjacency matrix or by adjacency list. There are some other ways to represent graph. One of them is to write the degrees (the numbers of edges that a vertex has) of each vertex. If there are n vertices then n integers
can represent that graph. In this problem we are talking about simple graph which does not have same endpoints for more than one edge, and also does not have edges with the same endpoint.
Any graph can be represented by n number of integers. But the reverse is not always true. If you are given n integers, you have to find out whether this n numbers
can represent the degrees of n vertices of a graph.
Input
Each line will start with the number n (≤ 10000). The next n integers will represent the degrees of n vertices of the graph. A 0 input for n will indicate end of input which should not be processed.
Output
If the n integers can represent a graph then print “Possible”. Otherwise print “Not possible”. Output for each test case should be on separate line.
Sample Input
Output for Sample Input
4 3 3 3 3
6 2 4 5 5 2 1
5 3 2 3 2 1
0
Possible
Not possible
Not possible
[align=left]Problemsetter: Md. Bahlul Haider
Judge Solution: Mohammed Shamsul Alam
Special thanks to Tanveer Ahsan
[/align]

题意:给出N个顶点的度数,判断这N个顶点是否构成图。

思路:为保证其尽可能为图,要优先考虑大度数之间的点优先连接。在这里可以采取依次减度数的方法推断能否构成图。如:3,3,2,2,1五个点,从度数最大的开始考虑,先去掉3,则后面紧接的三个点度数依次减去1,剩下的四个点排序后为2,1,1,1,再去掉度数最大的2,后面的两个点一次减去1,排序后剩下三点为1,0,0,1后的一点度数再减去1,则为-1,明显不成立,为Not
possible。

ps:简直坑,提交了好多次,总是WA,找了好久的bug,发现最后的Not possible 中的possible首字母打成了大写!!血的教训,太粗心了,下次注意!

代码:

#include<iostream>
#include<algorithm>
#include<string>

using namespace std;

class Node
{
public:
string str;
int cost;
}node[10005];

bool cmp(Node s1,Node s2)
{
if(s1.cost!=s2.cost) return s1.cost<s2.cost;
else return s1.str<s2.str;
}

int main()
{
int num,pos;
cin>>num;
for(pos=1;pos<=num;pos++)
{
int x,y,z,r,t;
cin>>x>>y>>z;
char ch;
for(int i=0;i<z;i++)
{
int work=x;
string st="";
while(cin>>ch&&ch!=':')
{
st=st+ch;
}
node[i].str=st;
cin>>r>>ch>>t;
int val=0;
while(t<((work+1)/2*r)&&(work-(work+1)/2)>=y)
{
val=val+t;
work=work-(work+1)/2;
}
val=val+(work-y)*r;
node[i].cost=val;
}
sort(node,node+z,cmp);
cout<<"Case "<<pos<<endl;
for(int i=0;i<z;i++)
{
cout<<node[i].str<<" "<<node[i].cost<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: