B - New Year Permutation
来源:互联网 发布:wifi局域网劫持软件 编辑:程序博客网 时间:2024/04/30 13:15
#include <iostream>
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define MAXN 333
#define INF 1111111
struct node
{
int val, id;
bool operator <(const node &b) const
{
if(val != b.val)
return val > b.val;
return id > b.id;
}
};
priority_queue<node> que;
int ans[MAXN], n;
int dis[MAXN][MAXN];
void fan()
{
for( int k = 0; k < n; k++)
{
for( int i = 0; i < n; i++)
{
for( int j = 0; j < n; j++)
{
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
int main()
{
while(scanf("%d",&n) != EOF)
{
memset(ans, -1, sizeof(ans));
if(!que.empty())
que.pop();
node temp;
for( int i = 0; i < n; i++)
{
scanf("%d",&temp.val);
temp.id = i;
que.push(temp);
}
char a;
for( int i = 0; i < n; i++)
{
getchar();
for( int j = 0; j < n; j++)
{
scanf("%c",&a);
dis[i][j] = a - '0';
if(dis[i][j] == 0 && i != j)
dis[i][j] = INF;
}
}
fan();
int x = 0;
int head;
while(que.size())
{
temp = que.top();
que.pop();
head = x;
//printf("-----%d---%d----",temp.id, x);
//printf("%d\n", dis[temp.id][x]);
while(dis[temp.id][x] == INF || ans[x] != -1)
x++;
ans[x++] = temp.val;
x = (x == head+1? x : head);
}
for( int i = 0; i < n; i++)
printf("%d%c",ans[i], i == n -1 ? '\n' :' ');
}
}
#include <cstdio>
#include <queue>
#include <cstring>
using namespace std;
#define MAXN 333
#define INF 1111111
struct node
{
int val, id;
bool operator <(const node &b) const
{
if(val != b.val)
return val > b.val;
return id > b.id;
}
};
priority_queue<node> que;
int ans[MAXN], n;
int dis[MAXN][MAXN];
void fan()
{
for( int k = 0; k < n; k++)
{
for( int i = 0; i < n; i++)
{
for( int j = 0; j < n; j++)
{
if(dis[i][j] > dis[i][k] + dis[k][j])
dis[i][j] = dis[i][k] + dis[k][j];
}
}
}
}
int main()
{
while(scanf("%d",&n) != EOF)
{
memset(ans, -1, sizeof(ans));
if(!que.empty())
que.pop();
node temp;
for( int i = 0; i < n; i++)
{
scanf("%d",&temp.val);
temp.id = i;
que.push(temp);
}
char a;
for( int i = 0; i < n; i++)
{
getchar();
for( int j = 0; j < n; j++)
{
scanf("%c",&a);
dis[i][j] = a - '0';
if(dis[i][j] == 0 && i != j)
dis[i][j] = INF;
}
}
fan();
int x = 0;
int head;
while(que.size())
{
temp = que.top();
que.pop();
head = x;
//printf("-----%d---%d----",temp.id, x);
//printf("%d\n", dis[temp.id][x]);
while(dis[temp.id][x] == INF || ans[x] != -1)
x++;
ans[x++] = temp.val;
x = (x == head+1? x : head);
}
for( int i = 0; i < n; i++)
printf("%d%c",ans[i], i == n -1 ? '\n' :' ');
}
}
0 0
- B - New Year Permutation
- B. New Year Permutation
- Codeforces 500B - New Year Permutation (思维)
- Good Bye 2014--B. New Year Permutation
- codeforces #500B# New Year Permutation
- Good Bye 2014 B. New Year Permutation
- B. New Year Permutation (CF)
- CF 500B New Year Permutation
- coderforce 500B New Year Permutation
- Codeforces 500B New Year Permutation
- 500B New Year Permutation(Floyd)
- CF - 500B New Year Permutation
- CodeForces 500B New Year Permutation
- Codeforces 500B New Year Permutation [贪心]
- 【CodeForces 500B】【贪心】New Year Permutation
- CF_500B New Year Permutation
- New Year Permutation
- New Year Permutation
- R语言记录
- JSP中静态include和动态include的区别
- 恩典都用
- C# 基础概念理解
- LeetCode Num64_Minimum Path Sum
- B - New Year Permutation
- 机房重构(一)--UML图
- html5Geolocation地理定位
- JSP中的pageEncoding和contentType的释疑
- Fibonacci Heap实现
- jquerymobile按钮事例
- wndr3800 经常出现2.4G无法连接的问题
- 实现一个插入,删除,随机都是O(1)复杂度的Set
- 错误:Error: Cannot retrieve metalink for repository: epel. Please verify its path and try again