poj 3159
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=30000+10;
const int maxm=150000+10;
int n,m;
int x,y,c;
struct Edge{
int to,cost,next;
bool operator < (const Edge &b) const{
return cost>b.cost;
}
}edge[maxm];
int head[maxn],cntE=0;
int d[maxn];
bool vis[maxn];
void init(){
cntE=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w){
edge[cntE]=(Edge){v,w,head[u]};
head[u]=cntE++;
}
void dijkstra(int start){
for (int i=1;i<=n;i++) d[i]=inf,vis[i]=false;
d[start]=0;
priority_queue <Edge> que;
que.push((Edge){start,0,0});
while (!que.empty()){
Edge tmp=que.top();
que.pop();
int u=tmp.to;
if (vis[u]) continue;
vis[u]=true;
for (int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if (!vis[v]&&d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
tmp=(Edge){v,d[v],0};
que.push(tmp);
}
}
}
}
int main(){
init();
scanf("%d %d",&n,&m);
for (int i=1;i<=m;i++){
scanf("%d %d %d",&x,&y,&c);
addedge(x,y,c);
}
dijkstra(1);
printf("%d\n",d[n]);
return 0;
}
poj 3169
#include
#include
#include
#include
#include
using namespace std;
const int inf=0x3f3f3f3f;
const int maxn=1000+10;
const int maxm=20000+10;
int n,m1,m2;
int x,y,c;
struct Edge{
int to,cost,next;
bool operator < (const Edge &b) const{
return cost>b.cost;
}
}edge[maxm];
int head[maxn],cntE=0;
int d[maxn];
bool vis[maxn];
int cnt[maxn];
void init(){
cntE=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w){
edge[cntE]=(Edge){v,w,head[u]};
head[u]=cntE++;
}
bool spfa(int start){
for (int i=1;i<=n;i++) d[i]=inf,vis[i]=false,cnt[i]=0;
d[start]=0,cnt[start]=1,vis[start]=true;
queue <int> q;
q.push(start);
while (!q.empty()){
int u=q.front();
q.pop();
vis[u]=false;
for (int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if (d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if (!vis[v]){
if (++cnt[v]>n) return false;
vis[v]=true;
q.push(v);
}
}
}
}
return true;
}
void dijkstra(int start){
for (int i=1;i<=n;i++) d[i]=inf,vis[i]=false;
d[start]=0;
priority_queue <Edge> que;
que.push((Edge){start,0,0});
while (!que.empty()){
Edge tmp=que.top();
que.pop();
int u=tmp.to;
if (vis[u]) continue;
vis[u]=true;
for (int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if (!vis[v]&&d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
tmp=(Edge){v,d[v],0};
que.push(tmp);
}
}
}
}
int main(){
init();
scanf("%d %d %d",&n,&m1,&m2);
for (int i=1;i<=m1;i++){
scanf("%d %d %d",&x,&y,&c);
addedge(x,y,c);
}
for (int i=1;i<=m2;i++){
scanf("%d %d %d",&x,&y,&c);
addedge(y,x,-c);
}
bool ans=spfa(1);
if (!ans) printf("-1\n");
else if (d[n]==inf) printf("-2\n");
else printf("%d\n",d[n]);
return 0;
}
poj 1716
#include
#include
#include
#include
#include
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=50000+10;
const int maxm=150000+10;
int n;
int a[maxn],b[maxn];
int maxx=0;
struct Edge{
int to,cost,next;
bool operator < (const Edge &b) const{
return cost>b.cost;
}
}edge[maxm];
int head[maxn],cntE=0;
int d[maxn],cnt[maxn];
bool vis[maxn];
void init(){
cntE=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w){
edge[cntE]=(Edge){v,w,head[u]};
head[u]=cntE++;
}
bool spfa(int start){
for (int i=0;i<=maxx;i++) d[i]=INF,vis[i]=false,cnt[i]=0;
d[start]=0,vis[start]=true,cnt[start]=1;
queue <int> que;
que.push(start);
while (!que.empty()){
int u=que.front();
que.pop();
vis[u]=false;
for (int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if (d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if (!vis[v]){
if (++cnt[v]>maxx) return false;
vis[v]=true;
que.push(v);
}
}
}
}
return true;
}
int main(){
/*
f[a[i]-1]-f[b[i]]<=-c[i]
f[i+1]-f[i]<=1
f[i]-f[i+1]<=0
*/
init();
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d %d",&a[i],&b[i]);
a[i]++;
b[i]++;
maxx=max(maxx,b[i]);
addedge(a[i]-1,b[i],-2);
}
for (int i=0;i<maxx;i++){
addedge(i+1,i,1);
addedge(i,i+1,0);
}
spfa(0);
printf("%d\n",-d[maxx]);
}
poj 1201
#include
#include
#include
#include
#include
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=50000+10;
const int maxm=150000+10;
int n;
int a[maxn],b[maxn],c[maxn];
int maxx=0;
struct Edge{
int to,cost,next;
bool operator < (const Edge &b) const{
return cost>b.cost;
}
}edge[maxm];
int head[maxn],cntE=0;
int d[maxn],cnt[maxn];
bool vis[maxn];
void init(){
cntE=0;
memset(head,-1,sizeof(head));
}
void addedge(int u,int v,int w){
edge[cntE]=(Edge){v,w,head[u]};
head[u]=cntE++;
}
bool spfa(int start){
for (int i=0;i<=maxx;i++) d[i]=INF,vis[i]=false,cnt[i]=0;
d[start]=0,vis[start]=true,cnt[start]=1;
queue <int> que;
que.push(start);
while (!que.empty()){
int u=que.front();
que.pop();
vis[u]=false;
for (int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if (d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if (!vis[v]){
if (++cnt[v]>n) return false;
vis[v]=true;
que.push(v);
}
}
}
}
return true;
}
int main(){
/*
f[a[i]-1]-f[b[i]]<=-c[i]
f[i+1]-f[i]<=1
f[i]-f[i+1]<=0
*/
init();
scanf("%d",&n);
for (int i=1;i<=n;i++){
scanf("%d %d %d",&a[i],&b[i],&c[i]);
maxx=max(maxx,b[i]);
addedge(a[i]-1,b[i],-c[i]);
}
for (int i=0;i<maxx;i++){
addedge(i+1,i,1);
addedge(i,i+1,0);
}
spfa(0);
printf("%d\n",-d[maxx]);
}
hdu 6252
#include
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int maxn=2000+10;
const int maxm=10000+10;
int T;
int n,m;
int kase=0;
ll x;
int cnt[maxn];
ll d[maxn];
int A,B,C,D;
bool vis[maxn];
ll ans[maxn];
struct Edge{
int to,next;
ll cost;
Edge(){}
Edge(int _to,ll _cost,int _next):to(_to),cost(_cost),next(_next){}
}edge[maxm];
int head[maxn],cntE=0;
void init(){
memset(head,-1,sizeof(head));
cntE=0;
}
void addedge(int u,int v,ll cost){
edge[cntE]=Edge(v,cost,head[u]);
head[u]=cntE++;
}
bool spfa(int start){
for (int i=1;i<=n;i++) d[i]=INF,cnt[i]=0,vis[i]=false;
d[start]=0,cnt[start]=1,vis[start]=true;
queue <int> que;
que.push(start);
while (!que.empty()){
int u=que.front();
que.pop();
vis[u]=false;
for (int i=head[u];i!=-1;i=edge[i].next){
int v=edge[i].to;
if (d[v]>d[u]+edge[i].cost){
d[v]=d[u]+edge[i].cost;
if (!vis[v]){
if (++cnt[v]>n) return false;
vis[v]=true;
que.push(v);
}
}
}
}
return true;
}
int main(){
scanf("%d",&T);
while (T--){
init();
scanf("%d %d %lld",&n,&m,&x);
for (int i=1;i<=m;i++){
scanf("%d %d %d %d",&A,&B,&C,&D);
if (A==B&&C==D){
addedge(A,C,-x);
addedge(C,A,x);
}
else{
addedge(A,D,-x-1);
addedge(C,B,x-1);
}
}
for (int i=1;i<=n-1;i++){
addedge(i,i+1,-1);
}
bool flag=spfa(1);
for (int i=1;i<=n-1;i++){
ans[i]=d[i]-d[i+1];
}
if (!flag) printf("Case #%d: IMPOSSIBLE\n",++kase);
else{
printf("Case #%d:",++kase);
for (int i=1;i<=n-1;i++){
printf(" %lld",ans[i]);
}
putchar('\n');
}
}
}